# 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.

Canonical: https://scaplet.com/seo/django/404

Guide Django Django · Error pages

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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fdjango%2F404%20(%22Django%20custom%20404%20page%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fdjango%2F404%20(%22Django%20custom%20404%20page%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fdjango%2F404%20(%22Django%20custom%20404%20page%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fdjango%2F404%20(%22Django%20custom%20404%20page%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

On this page

## 4 parts, 3 minutes.

1. [The quickest custom 404: a template 01](#the-quickest-custom-404-a-template)
2. [A custom handler with handler404 02](#a-custom-handler-with-handler404)
3. [Why the status code matters to Google 03](#why-the-status-code-matters-to-google)
4. [Test it 04](#test-it)

_01_Check 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>
```

_02_Good 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)
```

Set status=404 yourself

The `render()` shortcut's `status` parameter defaults to 200. A handler that calls `render(request, "404.html")` without `status=404` serves an error page with a success code. That is a [soft 404](/glossary/soft-404).

_03_Soft 404 versus a real 404

## Why the status code matters to Google

Setup

### Error page with `status=404`

Status

404

What Google does

Google crawlers tell the next system the content doesn't exist; an indexed URL is removed from the index

Setup

### Error page served with default `render()`

Status

200

What Google does

Reported as a soft 404 in [Search Console](/glossary/google-search-console)

Setup

### Page moved to a new URL

Status

301

What Google does

Redirect to the new location instead

Google defines a soft 404 as a URL that returns a page telling the user the page does not exist and also a 200 (success) [status code](/glossary/http-status-codes). Its fix for removed pages with no replacement is to return a 404 or 410. For moved pages, return a 301. See the [soft 404 glossary entry](/glossary/soft-404) and the [soft 404 errors](/seo-issues/soft-404-errors) issue.

Google's guidance on custom 404 pages: since they are useless from a search engine's perspective, make sure the server returns a 404 status code to prevent having them indexed. Return a 404 or 410 only when no replacement exists, and a 301 when there is one.

_04_How 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](/glossary/page-indexing-report) for soft 404 entries.

Questions

## Asked a lot.

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

_01_ How 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.

_02_ Why 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.

_03_ Does 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.

_04_ What 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. 01 [Writing views: customizing error views | Django documentation](https://docs.djangoproject.com/en/5.2/topics/http/views/) docs.djangoproject.com · checked 2026-10-24
2. 02 [Built-in Views: the 404 (page not found) view | Django documentation](https://docs.djangoproject.com/en/5.2/ref/views/) docs.djangoproject.com · checked 2026-10-24
3. 03 [django.urls utility functions: handler404 | Django documentation](https://docs.djangoproject.com/en/5.2/ref/urls/) docs.djangoproject.com · checked 2026-10-24
4. 04 [Django shortcut functions | Django documentation](https://docs.djangoproject.com/en/5.2/topics/http/shortcuts/) docs.djangoproject.com · checked 2026-10-24
5. 05 [Troubleshoot Google Search crawling errors | Google Search Central](https://developers.google.com/search/docs/crawling-indexing/troubleshoot-crawling-errors) developers.google.com · checked 2026-10-24
6. 06 [HTTP status codes, network and DNS errors | Google Search Central](https://developers.google.com/search/docs/crawling-indexing/http-network-errors) developers.google.com · checked 2026-10-24

## Keep learning

[All platform guides](/seo)

[Learn WordPress 404 page CMS](/seo/wordpress/404) [Related Soft 404 Crawling](/glossary/soft-404) [Read next Astro 404 page Frameworks](/seo/astro/404) [Do Run a free site audit Free, about two minutes](/site-audit)

Nearby in platform guides
[Cloudflare 301 redirects](/seo/cloudflare/redirects) [Contentful SEO](/seo/contentful) [Docusaurus AI crawlers and robots.txt](/seo/docusaurus/ai-crawlers) [Docusaurus XML sitemap](/seo/docusaurus/xml-sitemap)

Terms on this page
[Soft 404](/glossary/soft-404) [Google Search Console](/glossary/google-search-console) [HTTP status codes](/glossary/http-status-codes) [Page indexing report](/glossary/page-indexing-report)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
