在我的一个观点中,我有代码:
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/