Django renders the template `500.html` instead of `404.html`

孤街醉人 提交于 2019-12-10 17:13:35

问题


In one of my views I have the code:

raise Http404

When DEBUG=False Django renders the template 500.html instead of the correct 404.html! I can't understand why!

EDIT: When DEBUG=True I get the standard one (by 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.

What's more is that in the runserver console I see clearly the 404 code. Instead, when setting DEBUG=False in the console I get a 500!! This is so odd.

EDIT 2: If I place a print statement just before raising Http404 I see the message when DEBUG=True but not when it is False!

EDIT 3: I can confirm that when DEBUG=False the statement raise Http404 is never reached. How can it be possible??

Update

Actually with DEBUG=False at every URL I get a 500. While with DEBUG=True this does not happen. How can it possibly be?? It should run the same. I'm starting to think it is a Django bug.


回答1:


Your error with debug=false may be due to the allowed_hosts setting.

see : Setting DEBUG = False causes 500 Error

It is new in 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



来源:https://stackoverflow.com/questions/17382318/django-renders-the-template-500-html-instead-of-404-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!