1

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}} 

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.