Centos – 500 Internal Server Error in django static files on Apache

apache-2.2centosdjangopython

Recently I deployed a Django powered application but I have some problem with static files. Sometimes (and only sometimes), I get 500 Internal Server Error in getting static files such as css/js/image. Please take look at this images:

First request:

enter image description here

Second:

Second request

and last request:

First request

You can see that in three separate requests, I got different result in static files.

Apache Log:

[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93]   File "/usr/lib/python2.6/site-packages/django/utils/decorators.py", line 91, in _wrapped_view, referer: http://mydomain.biz/static/style/main.css
[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93]     response = view_func(request, *args, **kwargs), referer: http://mydomain.biz/static/style/main.css
[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93]   File "/usr/lib/python2.6/site-packages/django/views/defaults.py", line 32, in server_error, referer: http://mydomain.biz/static/style/main.css
[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93]     t = loader.get_template(template_name) # You need to create a 500.html template., referer: http://mydomain.biz/static/style/main.css
[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93]   File "/usr/lib/python2.6/site-packages/django/template/loader.py", line 145, in get_template, referer: http://mydomain.biz/static/style/main.css
[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93]     template, origin = find_template(template_name), referer: http://mydomain.biz/static/style/main.css
[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93]   File "/usr/lib/python2.6/site-packages/django/template/loader.py", line 138, in find_template, referer: http://mydomain.biz/static/style/main.css
[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93]     raise TemplateDoesNotExist(name), referer: http://mydomain.biz/static/style/main.css
[Wed Oct 10 16:00:08 2012] [error] [client 37.98.23.93] TemplateDoesNotExist: 500.html, referer: http://mydomain.biz/static/style/main.css

I have Apache 2.* and Django 1.4.1 with Python 2.6 on CentOS 5.

Best Answer

Problem solved but I don't know why. Previously, I used this route for static files:

urlpatterns += staticfiles_urlpatterns()

and after set this custom route, everything is fine:

urlpatterns += patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/home/path_to/static'}),
)
Related Topic