0

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.

manosim
  • 3,630
  • 12
  • 45
  • 68

2 Answers2

2

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 ...

Community
  • 1
  • 1
Brendan
  • 18,771
  • 17
  • 83
  • 114
  • Right; I'd check if your STATIC_URL in settings.py is relative (is it "static/" or "../static/" or something like that?) – Joel Burton Mar 18 '14 at 18:51
  • I get your point. I thought the same thing but in my base.html file all stylesheets and js files are linked like this 'href="{{STATIC_URL}}css/bootstrap.min.css"'. @JoelBurton I got this: STATIC_URL = '/static/' and STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"),) – manosim Mar 18 '14 at 18:55
  • It might be helpful for you to post the *rendered* head sections you get when visiting your two URLs. – Joel Burton Mar 18 '14 at 19:01
  • Here is registration.html [link](http://pastebin.com/bGVcYYYA) and here is dashboard.html [link](http://pastebin.com/UbtFf48d) (Thanks a lot for helping!) – manosim Mar 18 '14 at 19:05
  • I think what Brendad said is correct. So the correct use is: `{% static "css/bootstrap.min.css" %}` and NOT `{{STATIC_URL}}css/style.css`. Right? – manosim Mar 18 '14 at 19:11
  • Hmm generally the settings variables are not available by default in the template, it depends on if you passed them to the template in `views.py` in the context - maybe your view does not do this? See [this question](http://stackoverflow.com/questions/433162/can-i-access-constants-in-settings-py-from-templates-in-django) for more details ... – Brendan Mar 18 '14 at 19:12
  • Thanks a lot @Brendan and everyone! I'll check the link you gave me too! – manosim Mar 18 '14 at 19:15
  • @Manolis: as others have mentioned, it looks like your broken template is getting an empty value for {{STATIC_URL}} or, more likely, it's not being passed at all (annoyingly, Django renders missing variables as an empty string). Moving to the newer `{% static 'css/bootstrap.min.css' %}` can remove this kind of buglet from biting you. To use the `{% static ... %}` tag, you'll need `{% load staticfiles %}` at the top of your template. – Joel Burton Mar 18 '14 at 19:31
0

(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?)

Joel Burton
  • 1,466
  • 9
  • 11
  • Hi Joel. Just posted an update to the thread which explains that. Both templates are really simple. You can see those at [link](http://pastebin.com/54ufjR22) and [link](http://pastebin.com/efc0hfq0) – manosim Mar 18 '14 at 18:48