Custom 404.html not loading

My custom 404.html, 500.html etc pages are not loading as intended. I have read lots about this and think I am doing it the correct way.

setting.py


DEBUG = False

ALLOWED_HOSTS = ['*']

# Templates Directory

TEMPLATE_DIR = os.path.join(BASE_DIR,"templates")

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

400.html


{% extends "tracker/index.html" %}

{% block title %}
400 - Error
{% endblock %}

{% block header %}  
400 - Error
{% endblock %}

{% block content %}
{% load static %}
<h4>Error</h4>

<div class="center-image">
    <img src="{% static "img/404.jpg" %}" alt="404.jpg" class="full-width">
    </div>

<p class="mt-2">Have you tried turning it off and on again. </p>

{% endblock %}

Folder structure

β”œβ”€β”€ nameofproject
β”‚ β”œβ”€β”€ init.py
β”‚ β”œβ”€β”€ pycache
β”‚ β”œβ”€β”€ asgi.py
β”‚ β”œβ”€β”€ settings.py
β”‚ β”œβ”€β”€ urls.py
β”‚ └── wsgi.py
β”œβ”€β”€ templates
β”‚ β”œβ”€β”€ 400.html
β”‚ β”œβ”€β”€ 403.html
β”‚ β”œβ”€β”€ 404.html
β”‚ β”œβ”€β”€ 500.html
β”‚ β”œβ”€β”€ desktop-index.html
β”‚ β”œβ”€β”€ mobile-index.html
β”‚ └── registration
β”œβ”€β”€ app1
β”œβ”€β”€ app2
└── app3

When any django app hits a problem, these custom pages don’t load. The default Django ones do instead.

For example

http://127.0.0.1:8000/hsadfjkldashf

Terminal

[22/Jan/2025 10:06:38] "GET /hsadfjkldashf HTTP/1.1" 404 179

What am I doing wrong?

Please show a directory listing (ls -l commands are fine) of your project-level dir and the templates dir.

Also, have you done any work to create a custom 404 view? (Not just the template)

Thanks Ken, updated above.

You’re showing your 400.html but not your 404.html.

400, 403, 404 and 500.html are copies of each other, just with the title changed at the moment.

For testing purposes, please replace your 404.html template with a very trivial html page - something like:

<html><head></head>
<body>
<h3>Page not found</h3>
<p>This was supposed to be a custom page, but we didn't find it.</p>
</body>
</html>

Hi Ken,

Yes that worked. Thanks! I have now worked out that the line that is causing my original html to not run is

{% extends "tracker/index.html" %}

And I now know that that is an incorrect path. I have changed it and it works.

Thanks for your help!

The annoying part is that Django didn’t give me any errors here (maybe due to the debug False setting) so I wasn’t able to diagnose this easily!

It’s either that or that the error is being thrown in an error handler which is already handling an error. But yea, that’s the type of thing you need to be careful about when customizing any of the error-handling code.

1 Like