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
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.
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.
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.
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.
<h1>Page not found</h1>
<p>We could not find {{ request_path }}.</p>
<p><a href="/">Go to the home page</a></p>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.handler404 = "mysite.views.custom_page_not_found"from django.shortcuts import render
def custom_page_not_found(request, exception):
return render(request, "404.html", status=404)status=404render()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. 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 and the 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.
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.
The 404 view is bypassed when DEBUG is True, so test with it off.
Run curl -I https://example.com/no-such-page/ and confirm the first line shows 404.
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__).
After deployment, watch the Page indexing report for soft 404 entries.
4 questions people ask next, answered from the sources on this page.
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.
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.
The default view does. A custom handler using render() returns 200 unless you pass status=404, which makes it a soft 404.
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.
Run the free audit, then approve the fixes. Every change can be undone.