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

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

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

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

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


当前回答

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 '。

其他回答

您可以使用WhiteNoise在生产环境中提供静态文件。

安装:

pip install WhiteNoise==2.0.6

把你的wsgi.py文件改成这样:

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

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

你可以开始了!

归功于Handlebar创意博客。

但是,真的不建议在生产环境中以这种方式提供静态文件。你的生产web服务器(比如nginx)应该处理这个问题。

我同意Marek Sapkota的回答;但是你仍然可以使用django URFConf来重新分配url,如果静态文件被请求的话。

步骤1:在settings.py中定义STATIC_ROOT路径

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

步骤2:然后收集静态文件

$ python manage.py collectstatic

步骤3:现在定义URLConf,如果static位于url的开头,则从静态文件夹staticfiles中访问文件。注意:这是你项目的urls.py文件:

from django.urls import re_path
from django.views.static import serve

urlpattern += [
  re_path(r'^static/(?:.*)$', serve, {'document_root': settings.STATIC_ROOT, })
]

这是您在终端上运行项目时必须键入的,而不需要DEBUG = TRUE 然后你看到所有资产(静态)文件加载正确在本地服务器上。

python manage.py runserver --insecure 

——insecure:这意味着你可以在没有安全模式的情况下运行服务器

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 '。

我对我的project/urls.py做了以下更改,它为我工作

加上这一行: 导入url

并补充: url (r ^媒体/ (? P <路径>。*)的美元,,{“document_root”:设置。MEDIA_ROOT}), urlpattern。