added message for permission denied exceptions #720
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I found myself wanting to pass a string to the
PermissionDenied
exceptions to that I could display more information to the user on the 403 page.This PR adds an attribute
permission_denied_message
and an overridable methodget_permission_denied_message
to the PermissionRequiredMixin which can be configured with a message string that is passed to the PermissionDenied exception. By default, Django will then add the string representation of thePermissionDenied
exception to the template context asexception
.If
permission_denied_message
is undefined, an empty string is passed toPermissionDenied
.Here are a couple of examples
If the current user does not have the
app_label.view_secretmodel
permission, they will be served the 403 page that contains the message "You do not have access to the request secret object.".models.py
and403.html
as above.