Free auditStart free
GuideDjango · Error pages

Django custom 404 page

Add a 404.html template or handler404 in Django, return a true 404 status, and avoid the soft 404 errors Google flags on error pages.

3 min
read
6
sources
Oct 24, 2026
updated
Scaplet Editorial Team
author
In one line

Create a 404.html template in your root template directory and Django serves it for unmatched URLs when DEBUG is False. For custom logic, set handler404 in the root URLconf to a view that accepts request and exception. Make sure it returns status 404, because render() defaults to 200.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01Check it

The quickest custom 404: a template

Django's docs say to create an HTML template named 404.html and place it at the top level of your template tree. It is served when DEBUG is False. With DEBUG = True, the 404 view is never used; Django shows a debug page listing your URLconf instead.

The default view, django.views.defaults.page_not_found(request, exception, template_name='404.html'), passes two context variables to the template: request_path, the URL that caused the error, and exception, a representation of the exception that triggered the view.

Raise Http404 in your own views when an object is missing. get_object_or_404() does this for you by raising Http404 instead of the model's DoesNotExist exception.

templates/404.html
<h1>Page not found</h1>
<p>We could not find {{ request_path }}.</p>
<p><a href="/">Go to the home page</a></p>
02Good to know

A custom handler with handler404

Set handler404 in your root URLconf only. Django's reference says it is a callable, or the full import path to the view called when no URL pattern matches, and a custom view must accept request and exception.
mysite/urls.py
handler404 = "mysite.views.custom_page_not_found"
mysite/views.py
from django.shortcuts import render


def custom_page_not_found(request, exception):
    return render(request, "404.html", status=404)
04How it works

Test it

Test pattern
from django.test import SimpleTestCase


class NotFoundTests(SimpleTestCase):
    def test_missing_url_returns_404(self):
        response = self.client.get("/no-such-page/")
        self.assertEqual(response.status_code, 404)

The same handler pattern exists for 500, 403, and 400 errors with handler500, handler403, and handler400. Check custom 404 pages for what a useful error page contains.

  1. 01

    Set DEBUG to False in a test environment

    The 404 view is bypassed when DEBUG is True, so test with it off.

  2. 02

    Request a missing URL

    Run curl -I https://example.com/no-such-page/ and confirm the first line shows 404.

  3. 03

    Add a test

    Use Django's test client and assert on response.status_code. Django's docs test a custom handler by routing a test URL to a view that raises the matching exception, with override_settings(ROOT_URLCONF=__name__).

  4. 04

    Check Search Console

    After deployment, watch the Page indexing report for soft 404 entries.

Questions

Asked a lot.

4 questions people ask next, answered from the sources on this page.

01How do I create a custom 404 page in Django?

Add a 404.html template at the top level of your templates and set DEBUG to False. Django uses it for unmatched URLs. For custom logic, point handler404 in the root URLconf at your own view.

02Why is my Django 404 page not showing?

With DEBUG = True, Django never uses the 404 view and shows a debug page instead. Set DEBUG to False and make sure 404.html is in your root template directory.

03Does my Django 404 page return a 404 status?

The default view does. A custom handler using render() returns 200 unless you pass status=404, which makes it a soft 404.

04What arguments does a handler404 view take?

request and exception. Django's docs say a custom view must accept both and return an HttpResponseNotFound; a response with status 404 works the same way.

Sources · 6

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • djangoproject.com
  • google.com
  1. 01Writing views: customizing error views | Django documentationdocs.djangoproject.com · checked 2026-10-24
  2. 02Built-in Views: the 404 (page not found) view | Django documentationdocs.djangoproject.com · checked 2026-10-24
  3. 03django.urls utility functions: handler404 | Django documentationdocs.djangoproject.com · checked 2026-10-24
  4. 04Django shortcut functions | Django documentationdocs.djangoproject.com · checked 2026-10-24
  5. 05Troubleshoot Google Search crawling errors | Google Search Centraldevelopers.google.com · checked 2026-10-24
  6. 06HTTP status codes, network and DNS errors | Google Search Centraldevelopers.google.com · checked 2026-10-24
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane