Why does DEBUG=False setting make my django Static Files Access fail?

Am building an app using Django as my workhorse. All has been well so far - specified db settings, configured static directories, urls, views etc. But trouble started sneaking in the moment I wanted to render my own beautiful and custom 404.html and 500.html pages.

I read the docs on custom error handling, and set necessary configurations in UrlsConf, created corresponding views and added the 404.html and the 500.html to my app's template directory (specified in the settings.py too).

But the docs say you can actually view custom error views until Debug is Off , so I did turn it off to test my stuff, and that's when stuff goes berserk!

Not only do I fail to view the custom 404.html (actually, it loads, but because my error pages each contain a graphic error message -as some nice image), the source of the erro page loads, but nothing else loads! Not even linked CSS or Javascript!

Generally, once I set DEBUG = False , all views will load, but any linked content (CSS, Javascript, Images, etc) wont load! What's happening? Is there something am missing about Static files and the Debug setting ?


关闭调试关闭Django不会再为你处理静态文件 - 你的生产Web服务器(Apache或其他)应该照顾到这一点。


如果你仍然需要在本地服务器静态(例如,在没有调试的情况下进行测试),你可以在不安全的模式下运行devserver:

manage.py runserver --insecure

You can use WhiteNoise to serve static files in production.

Install:

pip install WhiteNoise

And change your wsgi.py file to this:

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

And you're good to go!

Credit to Handlebar Creative Blog.

BUT, it's really not recommended serving static files this way in production. Your production web server(like nginx) should take care of that.

链接地址: http://www.djcxy.com/p/38726.html

上一篇: 如何在Django 1.4中自定义管理过滤器

下一篇: 为什么DEBUG = False设置让我的django静态文件访问失败?