How To Redirect To A 404 Page In A Django View

Redirecting in Django can be very simple and easy with the Django method get_object_or_404. But what if you are using something like an API and you don't have access to get_object_or_404? We will show you another simple way to redirect from a Django view using Http404. Basically, all you need to do is raise a 404 error. That fires the Django redirect to 404 page.

1
2
3
4
5
6
7
from django.http import Http404

def SomeView(request, param):
 if not param:
  raise Http404

 return render_to_response('someview.html')

Related Posts

0 Comments

12345

    00