500.html instead of 404.html

I have a main application with admin panel and direct_to_template homepage. When I type a url that does not match any of the URLs, I get a 404 error, but when I set DEBUG to false, I get 500.html instead of 404.html. Any idea why?

+8
source share
5 answers

Django caters for 500 when the view function fails, which means there must be an incorrect debugging failure.

The only way to tell is to see which django exception is being logged either through your server’s logs or by email sent by django to the ADMINS list.

+8
source

I would also check the TEMPLATE_DIRS path in settings.py, as that was the problem in my case. Django throws a 404 error, but since it could not find 404.html (in the path specified in TEMPLATE_DIRS), so it raised 500 errors - the template was not found (which either appears in the browser when debugging True, or in the server log when debugging = False).

+4
source

I faced the same problems. And I make the system send an error message to my gmail:

 SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): 127.0.0.1:8000 

So, I am adding this code to settings.py:

 ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] 

And now, everything is in order. I think that when you make DEBUG and TEMPLATE_DEBUG false, you should set ALLOWED_HOSTS.

Good luck.

+2
source

Maybe something is wrong in your 404.html file. For example, I use the extends tag, for example

 {% extends base.html%} 

in my file 404.html, then it always shows 'sever error 500' instead of 'page not found 404'. but when I correct my mistake, following

 {% extends "base.html" %} 

everything goes well.

+1
source

If anyone has ever encountered such an error, you can also write your opinion like this:

 def error_404(request, exception): data = {"name": "yoursite.com"} return render(request,'404.html', data) 

Without an exception argument this results in error 500 and, in preference, 500.html .

0
source

All Articles