500.html instead of 404.html
Asked Answered
H

6

8

I have basic application with admin panel and direct_to_template home page. When I type url which does not match any from urls I receive 404 error, but when I set DEBUG to false I receive 500.html instead of 404.html. Any idea why?

Harden answered 2/3, 2011 at 12:19 Comment(0)
L
9

Django serves a 500 when a view function fails, which means there must be some non debug specific code breaking.

The only way to tell is see what exception django is logging either via your server logs or the email that django sends to the ADMINS list.

Lulululuabourg answered 2/3, 2011 at 12:25 Comment(3)
You were right, I had error in 404.html. When debug was true it did not appear. Thanks a lot :)Harden
For me, it was a problem with the ALLOWED_HOSTS setting. Running the site on localhost:8000 with DEBUG=False requires that you add '*' to the ALLOWED_HOSTS list, or you'll get a 500 error. Be sure to remember to take it out again before deploying to a production server.Confound
it worked for me as there was an error into my 404.htmlMarie
E
4

I would also check TEMPLATE_DIRS path in settings.py as this was the problem in my case. Django raises 404 error, but as it was not able to find 404.html (in the path specified in TEMPLATE_DIRS) so it raised 500 error - template not found (which is either show in the browser when debug is True or server log, when debug=false).

Exculpate answered 8/7, 2012 at 3:56 Comment(0)
P
2

I have met the same problems. And I make the system send the error to my gmail:

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

So, I add this code in settings.py:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

And now, it's ok. I think when you make the DEBUG and TEMPLATE_DEBUG are false, you must set ALLOWED_HOSTS.

Good luck.

Pennyroyal answered 6/10, 2013 at 13:54 Comment(0)
D
2

If anyone ever comes across such an error then you may also consider writing your view this way:

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

Without the exception argument then it leads to error 500 and in preference 500.html is executed.

Dd answered 22/4, 2019 at 17:10 Comment(0)
A
1

Maybe there is something wrong in your 404.html.For example, I use extends tag like

{% extends base.html%}

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

{% extends "base.html" %}

everything goes well.

Analemma answered 19/8, 2013 at 7:55 Comment(0)
D
0

In my case, I had an invalid tag in my 404.html. That caused django to break somehow. There was not any traces in the log showing that

Dunkirk answered 30/8, 2023 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.