Why does Django generate HTTP 500 errors for static media when Debug is set to False?

447 views
Skip to first unread message

Huuuze

unread,
Sep 23, 2008, 11:53:08 AM9/23/08
to Django users
I'm preparing to deploy my Django app and I noticed that when I change
the "DEBUG" setting to False, all references to static files (i.e.,
JavaScript, CSS, etc..) result in HTTP 500 errors.

Any idea what's causing that issue (and how to fix it)?

Karen Tracey

unread,
Sep 23, 2008, 12:06:41 PM9/23/08
to django...@googlegroups.com
No. An idea for getting enough information to figure it out, though, is to configure things in settings.py (ADMINS, the various EMAIL_HOST, EMAIL_HOST_USER, etc. settings) so that you get the tracebacks for these errors mailed to you. 

Karen

Huuuze

unread,
Sep 23, 2008, 12:14:02 PM9/23/08
to Django users
There are no tracebacks in this instance. I can see the non-descript
500 errors appearing in my terminal window.

Karen Tracey

unread,
Sep 23, 2008, 12:29:26 PM9/23/08
to django...@googlegroups.com
On Tue, Sep 23, 2008 at 12:14 PM, Huuuze <huu...@orgoo.com> wrote:

There are no tracebacks in this instance.  I can see the non-descript
500 errors appearing in my terminal window.

I don't understand.  Are you saying you have configured ADMINS, etc. in settings.py so that 500 error tracebacks are mailed to you, but these particular ones are not?  What terminal window are you referring to?  Perhpas a cut and paste of what you are seeing, exactly, will help, as it is I am completely in the dark as to what is going on.

Plenty of people run with DEBUG off and are able to serve static media, so there's something particular about your setup that is causing this.  Are you testing this with the development server or are you using Apache or something else?  Do you have anything related to configuring serving of static media that is bracketed by "if debug is on" type statements?  This is not a commonly encountered problem so I can't give any general advice based on why others have run into this -- you need to provide more information about your setup if you want any real guidance on what might be causing this.

Karen

h

unread,
Sep 23, 2008, 12:36:54 PM9/23/08
to Django users
I suspect you have something like this in your urls.py and your static
media is not being served


if not settings.DEBUG:
urlpatterns += patterns('',(r'^media/(.*)$',
'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),)

Ramiro Morales

unread,
Sep 23, 2008, 2:11:06 PM9/23/08
to django...@googlegroups.com
On Tue, Sep 23, 2008 at 1:36 PM, h <hassas...@gmail.com> wrote:
>
> I suspect you have something like this in your urls.py and your static
> media is not being served
>
>
> if not settings.DEBUG:
> urlpatterns += patterns('',(r'^media/(.*)$',
> 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),)
>
>

I was going to suggest the same. If so, we should consider renaming the
http://docs.djangoproject.com/en/dev/howto/static-files/
document from "Serving static files" to "Serving static files with the
Django development server".
to avoid helping people making wrong assumptions based in
the generality of the document title.

Regards,

--
Ramiro Morales

Graham Dumpleton

unread,
Sep 23, 2008, 8:08:34 PM9/23/08
to Django users
How are you hosting Django? It would help to know if you are using
builtin development server or whether you are hosting under Apache
using mod_python or mod_wsgi. If under Apache then use of certain
Apache configuration settings can cause 500 errors to be returned
instead of 404 errors. No point explaining though if not using Apache.

Graham

Graham Dumpleton

unread,
Sep 23, 2008, 8:08:34 PM9/23/08
to Django users
How are you hosting Django? It would help to know if you are using
builtin development server or whether you are hosting under Apache
using mod_python or mod_wsgi. If under Apache then use of certain
Apache configuration settings can cause 500 errors to be returned
instead of 404 errors. No point explaining though if not using Apache.

Graham

On Sep 24, 2:14 am, Huuuze <huu...@orgoo.com> wrote:

Ross Dakin

unread,
Sep 24, 2008, 5:38:21 PM9/24/08
to Django users
Graham,

I am using Apache and mod_wsgi, and I have experienced that issue
before (500 errors for 404, et al.).

I fixed it with some tinkering, but don't remember how. Would you
mind explaining this problem a little?

Thanks,
Ross


On Sep 23, 5:08 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>
wrote:

Graham Dumpleton

unread,
Sep 24, 2008, 7:34:21 PM9/24/08
to Django users
On Sep 25, 7:38 am, Ross Dakin <rossda...@gmail.com> wrote:
> Graham,
>
> I am using Apache andmod_wsgi, and I have experienced that issue
> before (500 errors for 404, et al.).
>
> I fixed it with some tinkering, but don't remember how.  Would you
> mind explaining this problem a little?

Consider that you have Django mounted at root and static media at a
sub URL.

If someone tries to access a file in static media directory and it
doesn't exist, then Apache generates a 404 error. In Apache default
configuration, this would result in an internally generated generic
404 error response page being returned to client.

If however you have used in Apache configuration the ErrorDocument
directive for 404 at some point, possibly by enabling multi language
custom error documents, then when the 404 occurs, rather than
returning an internally generated generic 404 error response, Apache,
based on the value of the ErrorDocument directive may trigger a sub
request to a handler to generate the custom error document.

For example, if you had:

ErrorDocument 404 /404.html

it will trigger a sub request against URL /404.html.

Problem is that you have Django mounted at root and see such a request
gets routed into Django where they most like is now no handler for
that URL. What happens is that Django itself returns a 404 error
response. Apache, seeing the 404 for the sub request thinks something
nasty must have gone wrong as it would expect it to work, and so
returns a generic 500 error response for the original request instead
since it now can't generate a valid error response page for the
original 404.

A quite nasty variation of this is where the ErrorDocument was for a
401 error response in relation to failed Basic authentication. This is
because the client would then never see the 401 response and always
get a 500 error response instead.

This may not explain your situation, but is a problem that can occur
and it isn't really obvious what is happening.

Graham

> Thanks,
> Ross
>
> On Sep 23, 5:08 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>
> wrote:
>
> > How are you hosting Django? It would help to know if you are using
> > builtin development server or whether you are hosting under Apache
> > using mod_python ormod_wsgi. If under Apache then use of certain

shacker

unread,
Sep 24, 2008, 8:58:18 PM9/24/08
to Django users

On Sep 24, 4:34 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>
wrote:

> If however you have used in Apache configuration the ErrorDocument
> directive for 404 at some point, possibly by enabling multi language
> custom error documents, then when the 404 occurs, rather than
> returning an internally generated generic 404 error response, Apache,
> based on the value of the ErrorDocument directive may trigger a sub
> request to a handler to generate the custom error document.

We had a very similar problem with a wsgi setup on a cPanel machine.
cPanel defines the location of a bunch of Apache error documents
globally for all vhosts - error docs that don't exist in a Django
setup. The solution was to override those settings in the vhost
configuration for the site in question, like this:

ErrorDocument 401 "Authentication Error"
ErrorDocument 403 "Forbidden"

That way 401 and 403 return strings, rather than paths to non-existent
files, and the problem goes away.

Scot

Graham Dumpleton

unread,
Sep 25, 2008, 5:10:18 AM9/25/08
to Django users
Alternatively, add error pages in Django and set the ErrorDocument
directive to be the path that Django hosts the error page at.

Graham
Reply all
Reply to author
Forward
0 new messages