#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: | dev |
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.
Change History (6)
comment:1 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 11 years ago
Has patch: | set |
---|
comment:3 by , 11 years ago
Version: | 1.5 → master |
---|
comment:4 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
PR with docs and tests: https://github.com/django/django/pull/1416