我建立一个应用程序使用Django作为我的主力。到目前为止,一切都很好-指定的数据库设置,配置的静态目录,url,视图等。但是当我想要渲染自己漂亮的自定义404.html和500.html页面时,麻烦就来了。

我阅读了关于自定义错误处理的文档,并在UrlsConf中设置了必要的配置,创建了相应的视图,并将404.html和500.html添加到我的应用程序的模板目录(在settings.py中也指定了)。

但是文档说,在Debug关闭之前,您实际上可以查看自定义错误视图,所以我确实关闭了它来测试我的东西,而那就是事情变得疯狂的时候!

我不仅无法查看自定义404.html(实际上,它加载,但因为我的错误页面每个包含一个图形错误消息-作为一些漂亮的图像),错误页面的源加载,但没有其他加载!甚至没有链接CSS或Javascript!

一般来说,一旦我设置DEBUG = False,所有的视图将加载,但任何链接的内容(CSS, Javascript,图像等)不会加载!发生什么事情了?是否有什么东西我丢失了,关于静态文件和调试设置?


当前回答

在urls.py中,我添加了这一行:

from django.views.static import serve 

在urlpatterns中添加这两个url:

url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), 
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}), 

当DEBUG=FALSE时,静态文件和媒体文件都可以访问。

其他回答

我今天得到了这个问题,这在开发时解决了它,如果你仍然需要服务器本地静态(例如测试没有调试),你可以在不安全模式下运行devserver:

Manage.py runserver——不安全

不要担心,因为在生产环境中,这个托管平台(Apache、Heroku et c)将为您处理静态文件的服务。

注意:Heroku不服务器静态文件,你会想把它放在AWS或MS Azure上

最终解决方案: 所以当你设置debug = False时,Django不会关心你的静态文件。 所以我们想要一些可以处理我们文件的东西。 答案是白噪声。

pip install whitenoise in your environment Add 'whitenoise.middleware.WhiteNoiseMiddleware' in your middleware list in settings.py. This should be added just below the 'django.middleware.security.SecurityMiddleware' and above all the remaining middleware. So that your middleware list will look like this:- MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # add it exactlyhere 'django.contrib.sessions.middleware.SessionMiddleware', '...' ] Add 'whitenoise.runserver_nostatic' on top of your installed apps So that your installed apps list will look like this:- INSTALLED_APPS = [ 'whitenoise.runserver_nostatic', 'django.contrib.admin', 'django.contrib.auth', '...' ]

完成后,您现在就可以在生产环境中提供静态文件了!!

对url()的字符串视图参数的支持已弃用,并将在Django 1.10中被移除

我的解决方案只是上面孔拉多解决方案的小修正。

from django.conf import settings
import os
from django.views.static import serve as staticserve

if settings.DEBUG404:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', staticserve,
            {'document_root': os.path.join(os.path.dirname(__file__), 'static')} ),
        )

Johnny的回答很好,但仅仅通过添加那里描述的那些行对我来说仍然不适用。根据这个答案,对我有效的步骤是:

Install WhiteNoise as described: pip install WhiteNoise Create the STATIC_ROOT variable and add WhiteNoise to your MIDDLEWARE variable in settings.py: #settings.py MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', #add whitenoise 'django.contrib.sessions.middleware.SessionMiddleware', ... ] #... STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') ##specify static root Then, modify your wsgi.py file as explained in Johnny's answer: #wsgi.py from django.core.wsgi import get_wsgi_application from whitenoise.django import DjangoWhiteNoise application = get_wsgi_application() application = DjangoWhiteNoise(application) After that, deploy your changes to your server (with git or whatever you use). Finally, run the collectstatic option from your manage.py on your server. This will copy all files from your static folders into the STATIC_ROOT directory we specified before: $ python manage.py collectstatic You will now see a new folder named staticfiles that contains such elements.

在遵循这些步骤之后,您现在可以运行您的服务器,并且能够在生产模式下看到您的静态文件。

更新:如果你有版本< 4,更新日志表明不再需要声明WSGI_APPLICATION = 'projectName.wsgi。你的settings.py文件中的Application '。

实际上,你可以在Django生产应用程序中提供静态文件,而且不需要DEBUG=True。

不要使用Django本身,在你的WSGI文件(github)中使用dj_static:

让:

...
dj-static==0.0.6

YOURAPP/settings.py:

...
STATIC_ROOT = 'staticdir'
STATIC_URL = '/staticpath/'

YOURAPP/wsgi.py:

...
from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())