How do I raise a Response Forbidden in django
Return it from the view as you would any other response.
from django.http import HttpResponseForbidden
return HttpResponseForbidden()
if you want to raise an exception you can use:
from django.core.exceptions import PermissionDenied
def your_view(...):
raise PermissionDenied()
It is documented here :
https://docs.djangoproject.com/en/stable/ref/views/#the-403-http-forbidden-view
As opposed to returing HttpResponseForbidden
, raising PermissionDenied
causes the error to be rendered using the 403.html
template, or you can use middleware to show a custom "Forbidden" view.