Code

Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#20819 closed Cleanup/optimization (fixed)

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

Reported by: mrmachine 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

Description

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.

Attachments (0)

Change History (6)

comment:1 Changed 9 months ago by jezdez

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 9 months ago by mrmachine

  • Has patch set

comment:3 Changed 9 months ago by mrmachine

  • Version changed from 1.5 to master

comment:4 Changed 9 months ago by Tai Lee <tai.lee@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 4c6ffcf721b9a36b9b7e4730f4f4716cc90a5f02:

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

comment:5 Changed 9 months 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 9 months ago by Tim Graham <timograham@…>

In 4d8ecbdfda4da95685a2a51906425c6baa58a09f:

Fixed some ReST errors; refs #20819.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.