Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#20819 closed Cleanup/optimization (fixed)

`staticfiles.views.serve` should return 404 response instead of raising `ImproperlyConfigured`

Reported by: Tai Lee Owned by: nobody
Component: contrib.staticfiles Version: master
Severity: Normal Keywords: staticfiles serve ImproperlyConfigured
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I use this view to serve static files without having to collectstatic all the time during development, but I don't want to have code like this to create my root urlconf:

urlpatterns = patterns('',
    # urls

if 'runserver' in sys.argv:
    urlpatterns += patterns('',
        url(r'^static/(.*)', 'django.contrib.staticfiles.views.serve', name='static'),

urlpatterns += patterns('',
    # more urls. e.g. a catch-all that includes the URLs of another app at the root level.
    url(r'^', include('myapp.urls')),

The above example also means that the static URL cannot be reversed by name, because it's only present during development. In some cases it can be convenient to simply reverse a URL name like this directly from a template or Python code.

If I leave the URL in my root urlconf and deploy to production to avoid these issues, and I forget to collectstatic after an update or if visitors request a static file that really doesn't exist, I get an HTTP 500 error emailed to me.

This view shouldn't do anything in production, but I think it should be sufficient to simply return a 404 error instead of raising ImproperlyConfigured. This is still an accurate error (static files that do exist will be served by the front-end server), and developers should know that a 404 for static media in production means they need to run collectstatic.

I discussed this briefly with jezdez on IRC who seemed OK with this idea, as long as the documentation was clear that this view would return a 404 and not return static files if used in production.

Change History (6)

comment:1 Changed 5 years ago by Jannis Leidel

Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by Tai Lee

Has patch: set

comment:3 Changed 5 years ago by Tai Lee

Version: 1.5master

comment:4 Changed 5 years ago by Tai Lee <tai.lee@…>

Resolution: fixed
Status: newclosed

In 4c6ffcf721b9a36b9b7e4730f4f4716cc90a5f02:

Fixed #20819 -- Return 404 instead of 500 error when staticfiles view is used in production.

comment:5 Changed 5 years ago by Jannis Leidel <jannis@…>

In 0674b38ce904aa9d7a629313831ad24be3d8f3e6:

Merge pull request #1416 from thirstydigital/tickets/20819-staticfiles-serve-404

Fixed #20819 -- Return 404 instead of 500 error when staticfiles vie...

comment:6 Changed 5 years ago by Tim Graham <timograham@…>

In 4d8ecbdfda4da95685a2a51906425c6baa58a09f:

Fixed some ReST errors; refs #20819.

Note: See TracTickets for help on using tickets.
Back to Top