Free auditStart free
GuideError · HTTP status codes

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.

3 min
read
3
sources
Oct 30, 2026
updated
Scaplet Editorial Team
author
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
  2. Reproduce and check it in 2 minutes
  3. Raise Http404 from a view
  4. Customize the 404 page without breaking the status
  5. 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 withChatGPTClaudePerplexityGoogleAI Mode
02Check 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
03Good 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})
04Your 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)
05Explained

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, and for permission errors see 403 in Django.
Questions

Asked a lot.

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

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

02Where do I define handler404?

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

03Should 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. 01Django views: returning errors and customizing error viewsdocs.djangoproject.com · checked 2026-11-01
  2. 02Django exceptions referencedocs.djangoproject.com · checked 2026-11-01
  3. 03How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
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