Reputation: 1954
I attempted to raise a 403 error if the user accesses a page he is not allowed to access. In my views.py
def staff_room(request):
user = request.user
role = School.objects.get(user=user)
if not role.is_teacher:
raise PermissionDenied("Get out of the staff room!")
def library(request):
user = request.user
role = School.objects.get(user=user)
if not role.is_librarian:
raise PermissionDenied("Get out of the library!")
In my 403.html, I want to retrieve the different messages thrown by the errors. Is there a way to do so? Something like {{ exception.message }}
like say
{% extends 'base.html' %}
You are not allowed to enter this room. {{ exception.message}}
Upvotes: 1
Views: 759
Reputation: 1194
Django documentations tells us that for 403 errors it passes exception in the context like this
return http.HttpResponseForbidden(
template.render(request=request, context={'exception': force_text(exception)})
)
So it seems that you should be able to just use {{ exception }}
to access the exception's message. Otherwise you can override default 403 view and pass exception's message (or even format it) manually.
Upvotes: 3