Serving static files in development with Django

By Nick

Another small gotcha that took me some time to figure out.

I noticed my stylesheets and javascript weren’t loading during development. (note, I’m doing a small fun application starting from plain django, not pinax, like the previous thingy I was doing.) I did check my settings file to see if the MEDIA_ROOT was properly set, and it was. Still I got 404’s for stylesheets and javascripts.

After some triple checking paths and all, I went to google and found a nice explanation in the django documentation.

Basically, what you need to do is include the follwing url configuration in your root urls.py.

Be sure to read the documentation I mentioned above and the remarks they make about using this method only for development.

Hope this helps.

1
2
3
4
5
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': os.path.join(os.path.dirname(__file__), "site_media")}),
    )
Share This

Leave a Reply