About Forbidden Access

In a Django view, when a user does not have permission to modify an object, raise the PermissionDenied exception:

1
2
from django.core.exceptions import PermissionDenied
raise PermissionDenied

You can customize its output in the template 403.html.

If you want a custom content for that specific error message, return HttpResponseForbidden() instead:

1
2
from django.http import HttpResponseForbidden
return HttpResponseForbidden(content)

Tips and Tricks User Experience Django 4.2 Django 3.2 Django 2.2