class django.http.Http404
When you return an error such as HttpResponseNotFound
, you’re responsible for defining the HTML of the resulting error page:
return HttpResponseNotFound('<h1>Page not found</h1>')
For convenience, and because it’s a good idea to have a consistent 404 error page across your site, Django provides an Http404
exception. If you raise Http404
at any point in a view function, Django will catch it and return the standard error page for your application, along with an HTTP error code 404.
Example usage:
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})
In order to show customized HTML when Django returns a 404, you can create an HTML template named 404.html
and place it in the top level of your template tree. This template will then be served when DEBUG
is set to False
.
When DEBUG
is True
, you can provide a message to Http404
and it will appear in the standard 404 debug template. Use these messages for debugging purposes; they generally aren’t suitable for use in a production 404 template.
Please login to continue.