I spent several hours on this code and could not get to find out why I am getting server error (500) instead of 404.html in django program. Please see below codes and could someone help?
settings.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
urls.py: (app is mysite)
handler404 = "mysite.views.error_404"
views.py:
def error_404(request, exception):
return render(request, '404.html')
404.html:
{% extends "base.html" %}
{% load static %}
{% block main %}
some html text here
{% endblock %}
Please advise reason why I am not getting 404.html?
I got this problem this week and this site is one of the first results on google, i would like to answer the question here.
To solve this u need to change the nginx configuration to handle 404 erro.
In my case i had a 404.html page in /path/to/template and i added this lines to .conf file configuration insite server { } part
error_page 404 /404.html;
location = /404.html {
root /path/to/template ;
internal;
}
I ran into this exact issue and solved it after some struggling…
I don’t know if it’s still relevant in 2025, but I’m sharing the trick in case it may help somebody.
The solution was to pass a default null value to ‘exception’ parameter (exception=None):