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

In one of my views, I have the code:

raise Http404 

When DEBUG=False Django displays a 500.html template instead of the correct 404.html ! I do not 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 else in the runserver console runserver I see clearly 404 code. Instead , when setting DEBUG=False in the console, I get 500 !! This is so strange.

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

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

Update

Actually with DEBUG=False on every URL I get 500. Although with DEBUG=True this does not happen. How is this possible? It should work the same. I'm starting to think that this is a Django bug.

+4
source share
1 answer

Your error with debug = false may be caused by the allowed_hosts parameter.

see: Setting DEBUG = False causes a 500 error

This is new in django 1.5

ALLOWED_HOSTS needed for production

The new ALLOWED_HOSTS parameter checks host header requests and protects against host poisoning attacks. This parameter is now required when DEBUG is False, or django.http.HttpRequest.get_host () will raise SuspiciousOperation. For more information, see the complete documentation for the new setup.

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

+4
source

Source: https://habr.com/ru/post/1488878/


All Articles