[Answer]-My DJango app is responding to /static urls

1👍

It looks like you don’t have a URL pattern for /static. As such, the static/admin/css/base.css URL doesn’t match any pattern, and so you get a 404. Try something like this:

from django.conf.urls.static import static

# ...
urlpatterns = patterns('',
# ...
                       url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
                           {'document_root': settings.STATIC_ROOT}),
# ...

This should work for you — go to /static/foo.css, and you should see your CSS.

It’s worth noting that this is discouraged in a production environment. For your tutorial app, though, it’ll work.

👤Peter

0👍

The staticfiles app provides a custom runserver management command that automatically serves the static files, are you sure you have the following in your settings?

INSTALLED_APPS = (
    # ...
    'django.contrib.staticfiles',
)

In production, you’ll use the collectstatic management command that finds all of the static media and dumps it into STATIC_ROOT (this is the only purpose for this setting – it isn’t used or needed during development).

0👍

Glad you figured it out. Here’s why it works like this.

django.contrib.staticfiles overrides the runserver management command so that the static files are served automatically. To remind people that they shouldn’t be using django to serve static files, this only happens when DEBUG = True, as you found out.

The documentation of the overridden management command explains that you can use the --insecure flag to make this work no matter the state of the DEBUG setting.

Leave a comment