0 day streak
python / beginner
Snippet

Manual 404 Error Handling

You can manually trigger a 404 error page by raising the Http404 exception. This stops the view execution immediately.

snippet.py
python
1
2
3
4
5
from django.http import Http404
def product_detail(request, product_id):
if product_id > 1000:
raise Http404('Product does not exist')
django
Breakdown
1
from django.http import Http404
Imports the specific exception for Page Not Found errors.
2
raise Http404('Product does not exist')
Interrupts the request and displays the standard 404 error page.