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

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

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

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

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


当前回答

对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')} ),
        )

其他回答

您可以使用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)应该处理这个问题。

您可以用许多不同的方式调试它。这是我的方法。

localsettings.py:

DEBUG = False
DEBUG404 = True

urls . py:

from django.conf import settings
import os

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

请务必阅读文档;)

https://docs.djangoproject.com/en/2.0/howto/static-files/#limiting-use-to-debug-true

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

对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')} ),
        )

当我使调试= True我的静态是不工作。

如果我在python中运行我的项目manage.py runserver——不安全。这样我也得到了我的静电。

解决方案1:

python manage.py runserver --insecure

解决方案2:

但我需要永久的解决方案。然后我安装PIP install dj-static==0.0.6,并在我的wsgi.py文件中添加一些代码:

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

application = Cling(get_wsgi_application())

然后我在setting.py中添加了一些:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
STATICFILES_DIRS = [
    BASE_DIR / "static",
]