How to Create a Custom 404 View in Django
In Django, create a custom 404 view by defining a function that takes
request and exception parameters and returns a HttpResponseNotFound with your custom template. Then, set handler404 in your urls.py to point to this view and ensure DEBUG = False in settings.py for it to work.Syntax
To create a custom 404 view in Django, define a function with two parameters: request and exception. This function should return a HttpResponseNotFound or use render to display a 404 template. Then assign this function to handler404 in your urls.py.
request: The HTTP request object.exception: The exception that triggered the 404.HttpResponseNotFound: A response with HTTP status 404.handler404: A special variable inurls.pyto register the 404 view.
python
from django.http import HttpResponseNotFound from django.shortcuts import render def custom_404_view(request, exception): return render(request, '404.html', status=404) # In urls.py handler404 = 'myapp.views.custom_404_view'
Example
This example shows a complete custom 404 view that renders a friendly HTML page when a user visits a non-existent URL. It also shows how to configure handler404 in urls.py and the required settings.
python
# views.py from django.shortcuts import render def custom_404_view(request, exception): return render(request, '404.html', status=404) # urls.py from django.urls import path urlpatterns = [ path('', lambda request: render(request, 'home.html')), ] handler404 = 'myapp.views.custom_404_view' # settings.py DEBUG = False ALLOWED_HOSTS = ['localhost', '127.0.0.1'] # 404.html (template) # <html> # <head><title>Page Not Found</title></head> # <body><h1>Oops! Page not found.</h1><p>The page you requested does not exist.</p></body> # </html>
Output
When visiting a non-existent URL, the browser shows the 404.html page with status code 404 and message "Oops! Page not found."
Common Pitfalls
- Not setting
DEBUG = Falseinsettings.pywill cause Django to show its default debug 404 page instead of your custom one. - Forgetting to add your domain to
ALLOWED_HOSTScan block your custom 404 page from showing. - Not assigning
handler404correctly inurls.pywill keep the default 404 behavior. - Using a view without the
exceptionparameter will cause errors.
python
### Wrong: Missing exception parameter from django.shortcuts import render def bad_404_view(request): # Missing 'exception' return render(request, '404.html', status=404) ### Right: Include exception parameter from django.shortcuts import render def good_404_view(request, exception): return render(request, '404.html', status=404)
Quick Reference
- Define a 404 view with
requestandexceptionparameters. - Return a response with status 404 using
renderorHttpResponseNotFound. - Set
handler404inurls.pyto your view's path as a string. - Set
DEBUG = Falseand configureALLOWED_HOSTSinsettings.py.
Key Takeaways
Always define your custom 404 view with both request and exception parameters.
Set handler404 in urls.py to point to your custom 404 view as a string.
Ensure DEBUG is False and ALLOWED_HOSTS is set for the custom 404 page to display.
Use render() with status=404 to return a proper 404 response with a template.
Test your 404 page by visiting a non-existent URL after deployment.