python - Django 呈现模板 `500.html` 而不是 `404.html`

标签 python django http-status-code-404

在我的一个观点中,我有代码:

raise Http404

DEBUG=False 时,Django 呈现模板 500.html 而不是正确的 404.html!我不明白为什么!

编辑:当 DEBUG=True 我得到标准的(由 Django)

Page not Found (404)
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

更重要的是,在runserver控制台中,我清楚地看到了404代码。 相反,当在控制台中设置DEBUG=False 时,我得到一个500!!这太奇怪了。

编辑 2:如果我在引发 Http404 之前放置一个打印语句,我会在 DEBUG=True 时看到消息,但在 False 时不会>!

编辑 3:我可以确认当 DEBUG=False 语句 raise Http404 永远不会到达。怎么可能??

更新

实际上,在每个 URL 使用 DEBUG=False 时,我都会得到 500。而使用 DEBUG=True 时,这不会发生。怎么可能??它应该运行相同。我开始认为这是一个 Django 错误。

最佳答案

您的 debug=false 错误可能是由于 allowed_hosts 设置造成的。

参见:Setting DEBUG = False causes 500 Error

它是 django 1.5 中的新内容

ALLOWED_HOSTS required in production

The new ALLOWED_HOSTS setting validates the request’s Host header and protects against host-poisoning attacks. This setting is now required whenever DEBUG is False, or else django.http.HttpRequest.get_host() will raise SuspiciousOperation. For more details see the full documentation for the new setting.

https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed-hosts-required-in-production

关于python - Django 呈现模板 `500.html` 而不是 `404.html`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17382318/

相关文章:

c# - ASP.NET MVC3 : customErrors in Web. 配置在本地主机和服务器上被忽略,自定义错误页面仅显示在本地主机上

Python - 缩短 if/for 循环

python - 如何在 Django 表单中为 IntegerField 设置 NumberInput 小部件的宽度和输入限制

python - django-tables2/django-filter - 使用过滤器类进行分页

python - 在许多单独的线程中测试 Django 应用程序

java - ServletContextListener 问题 | HTTP 状态 404 -

jquery - 源 map 请求导致 404 错误

python - 收到类型错误 : can't multiply sequence by non-int of type 'str'

Python 开发环境,如 Smalltalk

Python 和 Java 参数传递