在 Django 中调试时静态文件不会加载
我正在创建一个 Django 项目。我只是尝试将项目退出调试,DEBUG = False
,由于某种原因,我的所有静态文件都没有显示。他们给出的错误代码为 500。我该如何解决这个问题?
一些settings.py:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
...
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
I'm creating a Django project. I just tried taking the project out of debug, DEBUG = False
and for some reason all my static files do not show up. They give an error code of 500. How do i fix this?
some of settings.py:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
...
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
静态文件应用不会在
DEBUG=False
模式下自动提供静态媒体。来自 django.contrib.staticfiles.urls :您可以通过手动附加到您的 urlpatterns 来提供它,或者使用服务器来提供静态文件(就像运行 Django 时应该做的那样)非调试模式下的项目)。
虽然我想知道的一件事是为什么你会得到 500 状态代码响应而不是 404。在这种情况下有什么例外?
编辑
因此,如果您仍然想通过 staticfiles 应用程序提供静态文件,请将以下内容添加到您的根 url 配置 (
urls.py
):您需要记住的一些事情不过:
STATIC_ROOT
(<代码>管理.py收集静态)。有关更多信息,请参阅 staticfiles 应用文档。由于您在非调试模式下运行,因此这是必要的。urls.py
中的from django.conf import settings
:)Static files app is not serving static media automatically in
DEBUG=False
mode. Fromdjango.contrib.staticfiles.urls
:You can serve it by appending to your
urlpatterns
manually or use a server to serve static files (like it is supposed to when running Django projects in non-DEBUG mode).Though one thing I am wondering is why you get a 500 status code response instead of 404. What is the exception in this case?
EDIT
So if you still want to serve static files via the staticfiles app add the following to your root url conf (
urls.py
):Some things you need to keep in mind though:
STATIC_ROOT
(manage.py collectstatic
). See the staticfiles app docs for more information. This is simply necessary since you run on non-Debug mode.from django.conf import settings
in yoururls.py
:)在Django 1.3中,如果您只是使用manage.py runserver进行测试,则可以添加选项“--insecure”,如staticfiles docs:
它似乎仍然向管理员发送电子邮件说没有模板,但确实如此提供静态文件。
我不确定电子邮件问题是故意的还是错误
In Django 1.3, if you are just testing using the manage.py runserver, you can add the option "--insecure", as described in the staticfiles docs:
It seems to still send emails to the admin saying that there is no template, but it does serve static files.
I'm not sure if the email issue is on purpose or a bug