# 404 Not Found in Django

> Why Django returns 404, how Http404 and HttpResponseNotFound differ, and how to set a 404.html or handler404 that returns the right status for crawlers.

Canonical: https://scaplet.com/errors/http/404/django

Guide Error · HTTP status codes

Why Django returns 404, how Http404 and HttpResponseNotFound differ, and how to set a 404.html or handler404 that returns the right status for crawlers.

3 min

read

3

sources

Oct 30, 2026

updated

Scaplet Editorial Team

author

404 4xx · Client error

The short answer

Django returns 404 when no URL pattern matches or when a view raises Http404. A custom page needs a 404.html template at the top of your template tree, and it is only used when DEBUG is False. Raise Http404 instead of returning a plain HttpResponse so the status stays 404.

1. [The two ways Django ends up at 404](#the-two-ways-django-ends-up-at-404)
2. [Reproduce and check it in 2 minutes](#reproduce-and-check-it-in-2-minutes)
3. [Raise Http404 from a view](#raise-http404-from-a-view)
4. [Customize the 404 page without breaking the status](#customize-the-404-page-without-breaking-the-status)
5. [What it means for SEO and crawlers](#what-it-means-for-seo-and-crawlers)

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%2Ferrors%2Fhttp%2F404%2Fdjango%20(%22404%20Not%20Found%20in%20Django%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%2Ferrors%2Fhttp%2F404%2Fdjango%20(%22404%20Not%20Found%20in%20Django%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%2Ferrors%2Fhttp%2F404%2Fdjango%20(%22404%20Not%20Found%20in%20Django%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%2Ferrors%2Fhttp%2F404%2Fdjango%20(%22404%20Not%20Found%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_Ways to signal a missing object from a view

## The two ways Django ends up at 404

Either the URL resolver finds no pattern for the path, or a view decides the [thing](/glossary/entity) you asked for does not exist. Both end at the same standard error page. The general meaning is on [404 Not Found](/errors/http/404); this page is about the Django side.

Approach

### raise Http404("message")

Status

404

Page shown

The application's standard error page (404.html when DEBUG is False)

Use when

Almost always; the docs call it the consistent option

Approach

### return HttpResponseNotFound("<h1>Page not found</h1>")

Status

404

Page shown

Only the HTML you pass; you define the whole page

Use when

You need full control of one response

Approach

### return HttpResponse("not found")

Status

200

Page shown

Whatever you render

Use when

Never for a missing page: crawlers see a successful response

Where the 404 is created

```
request -> URLconf (urls.py) |-- no pattern matches the path ------------> 404 \`-- pattern matches -> view |-- raise Http404 --> 404 page |-- return HttpResponseNotFound(...) --> 404, your HTML \`-- return HttpResponse(...) --> 200 even if the page says "not found"
```

_02_Check it

## Reproduce and check it in 2 minutes

Check what status a missing page returns, not what it looks like. This public endpoint is a known 404:

Run the same check against a path that should not exist on your site, and against a page your custom view renders for a missing record. This needs your own deployment:

Your own site: both lines should print 404

```
curl -s -o /dev/null -w "%{http_code}\n" https://your-site.example/this-path-does-not-exist/ curl -s -o /dev/null -w "%{http_code}\n" https://your-site.example/polls/999999/ # Expect: 404 and 404. A 200 on the second line means the view renders a message # without raising Http404.
```

Public endpoint: expect 404

```
curl -s -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/404 # Expect: 404
```

_03_Good to know

## Raise Http404 from a view

This is the pattern from the Django views documentation. When the lookup fails, raise `Http404`, and Django catches it and returns the standard error page with a 404 code.

views.py

```
from django.http import Http404 from django.shortcuts import render from polls.models import Poll def detail(request, poll_id): try: p = Poll.objects.get(pk=poll_id) except Poll.DoesNotExist: raise Http404("Poll does not exist") return render(request, "polls/detail.html", {"poll": p})
```

The message is for developers

With DEBUG on, the message you pass to Http404 appears in the debug template. The docs say these messages are for debugging and generally are not suitable for a production 404 template.

_04_Your call

## Customize the 404 page without breaking the status

Create a template named `404.html` at the top level of your template tree. Per the docs it is served when `DEBUG` is set to `False`. If you keep DEBUG on locally, you will see the debug page instead, which is why a custom 404 seems to be ignored in development.

If this fits

### Use 404.html alone if

- You only want a branded page
- The default view's status handling is fine for you

Or

### Write handler404 if

- You need different output for API paths and HTML paths
- You want to log or redirect on certain misses

urls.py: override the view

```
handler404 = "mysite.views.my_custom_page_not_found_view"
```

The docs state that `handler404` overrides the `page_not_found()` view and must be set in your URLconf: setting it anywhere else has no effect. A custom handler is your code, so give it the 404 status explicitly:

views.py: a handler that keeps the status

```
from django.shortcuts import render def my_custom_page_not_found_view(request, exception): return render(request, "404.html", status=404)
```

_05_Explained

## What it means for SEO and crawlers

Google removes a previously indexed URL from Search when it returns 404, and says crawl frequency for 404 pages gradually decreases. That is the correct outcome for a deleted page. The wrong outcome is a "not found" page that returns 200, because the status tells crawlers the page exists. Check the status of your custom page with the curl line above, and link visitors onward from it with working internal paths. For other 4xx handling see [4xx errors](/seo-issues/4xx-errors), and for permission errors see [403 in Django](/errors/http/403/django).

Questions

## Asked a lot.

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

_01_ Why does my custom 404.html not show in development?

Django serves 404.html when DEBUG is False. With DEBUG True it shows the debug 404 page and any message passed to Http404.

_02_ Where do I define handler404?

In your root URLconf. The Django docs say setting handlers anywhere else has no effect.

_03_ Should a custom not-found view return 200 with a friendly message?

No. Raise Http404 or return a response with status 404, so crawlers and browsers see that the page does not exist.

Sources · 3

## Every fact, linked and dated.

Official docs first. The date is when we last opened each page.

- djangoproject.com
- google.com

1. 01 [Django views: returning errors and customizing error views](https://docs.djangoproject.com/en/5.1/topics/http/views/) docs.djangoproject.com · checked 2026-11-01
2. 02 [Django exceptions reference](https://docs.djangoproject.com/en/5.1/ref/exceptions/) docs.djangoproject.com · checked 2026-11-01
3. 03 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-11-01

## Keep learning

[All errors](/errors)

[Learn 400 Bad Request in Django HTTP status codes](/errors/http/400/django) [Related 4xx errors Status codes and links](/seo-issues/4xx-errors) [Read next 403 Forbidden in Django HTTP status codes](/errors/http/403/django) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 404 errors
[GitHub Pages 404 Not Found](/errors/cdn/github-pages-404) [404 Not Found](/errors/http/404) [404 Not Found caused by .htaccess](/errors/http/404/htaccess) [404 Not Found in Flask](/errors/http/404/flask) [404 Not Found in IIS](/errors/http/404/iis)

More in 404 Not Found
[404 Not Found caused by .htaccess](/errors/http/404/htaccess) [404 Not Found in Django](/errors/http/404/django) [404 Not Found in Flask](/errors/http/404/flask) [404 Not Found in IIS](/errors/http/404/iis) [404 Not Found in WordPress](/errors/http/404/wordpress) [404 Not Found on Apache](/errors/http/404/apache) [404 Not Found on nginx](/errors/http/404/nginx)

Terms on this page
[Entity](/glossary/entity)

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
