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
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.
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.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
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"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:
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.curl -s -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/404
# Expect: 404Http404, and Django catches it and returns the standard error page with a 404 code.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})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.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:
from django.shortcuts import render
def my_custom_page_not_found_view(request, exception):
return render(request, "404.html", status=404)3 questions people ask next, answered from the sources on this page.
Django serves 404.html when DEBUG is False. With DEBUG True it shows the debug 404 page and any message passed to Http404.
In your root URLconf. The Django docs say setting handlers anywhere else has no effect.
No. Raise Http404 or return a response with status 404, so crawlers and browsers see that the page does not exist.
Run the free audit, then approve the fixes. Every change can be undone.