最近我部署了一个由 Django 提供支持的应用程序,但我遇到了一些静态文件问题。有时(而且只是有时),我会500内部服务器错误获取静态文件(如 css/js/image)。请看以下图片:
第一个请求:
第二:
最后一个请求:
您可以看到,在三个单独的请求中,我在静态文件中得到了不同的结果。
Apache 日志:
[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
我在 CentOS 5 上拥有 Apache 2.* 和 Django 1.4.1 以及 Python 2.6。
答案1
问题解决了,但我不知道为什么。以前,我对静态文件使用了以下路由:
urlpatterns += staticfiles_urlpatterns()
设置此自定义路线后,一切都很好:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/home/path_to/static'}),
)