Question

In a djano app, in urls.py, I got 2 urls. Both extend 'base.html'. But when I go to the url, one of them doesn't load the .css, .js files etc. Any ideas? Urls.py below:

from django.conf.urls import patterns, url
urlpatterns = patterns('',
    (r'^password/reset/$', 'django.contrib.auth.views.password_reset',
            {'post_reset_redirect' : '/membership/password/reset/done/'}),
    (r'^register/$', 'membership.views.register_user'),
)

Ps. I copied and pasted the "extends 'base.html' etc in order to avoid any typos.

Update I forgot to tell you that when the page loads, in the source code of the 'register' page it seems that 'static/' is missing. Even though in other pages example '/membership/dashboard/' it works properly.

Was it helpful?

Solution

These are served from two different 'directories' i.e. yourdomain.com/password/reset/ and yourdomain.com/register/. You very likely refer to you resources using a relative URL (which depend on the directory from which they are served) and so if you hardcoded the urls in base.html then at least one of them will be wrong ...

Django has a way to refer to the statically served resources such as html, javascript and so on in the templates which will use the correct relative URL depending on what you configure in urls.py.

e.g. taken from https://docs.djangoproject.com/en/dev/howto/static-files/

{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>

Update

It looks like you are relying on having access to values from settings.py inside the template to build your URLs which, by default, templates do not have access to. To get access you need to pass a context to your template from the view (see for example this question).

django.contrib.auth.views.password_reset probably does this and so the reference works, but your own view (i.e. membership.views.register_user) probably does not.

To pass STATIC_URL in the context, try in views.py:

def register_user(request, template="my_template.html"):
    context = { 'STATIC_URL' : settings.STATIC_URL }
    return render_to_response(template, context)

However building URLs like this is not the best way - it is probably best to switch to the Django sanctioned way if possible ...

OTHER TIPS

(I don't think you're going to get an answer based on your question. The problem is very unlikely to be related to your urls.py. It's more likely to be in the templates you're using. You can also clarify what you mean by "doesn't the load the .css, .js". Do you mean the rendered HTML doesn't have the and tags in it? Or that it does, but they point to the wrong thing or otherwise don't work?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top