Permission denied (403) handler. Templates: :template:`403.html` Context: exception The message from the exception which triggered the 403 (if one was supplied). If the template does not exist, an Http403 response containing the text "403 Forbid
(request, exception, template_name=ERROR_403_TEMPLATE_NAME)
| 124 | |
| 125 | @requires_csrf_token |
| 126 | def permission_denied(request, exception, template_name=ERROR_403_TEMPLATE_NAME): |
| 127 | """ |
| 128 | Permission denied (403) handler. |
| 129 | |
| 130 | Templates: :template:`403.html` |
| 131 | Context: |
| 132 | exception |
| 133 | The message from the exception which triggered the 403 (if one was |
| 134 | supplied). |
| 135 | |
| 136 | If the template does not exist, an Http403 response containing the text |
| 137 | "403 Forbidden" (as per RFC 9110 Section 15.5.4) will be returned. |
| 138 | """ |
| 139 | try: |
| 140 | template = loader.get_template(template_name) |
| 141 | except TemplateDoesNotExist: |
| 142 | if template_name != ERROR_403_TEMPLATE_NAME: |
| 143 | # Reraise if it's a missing custom template. |
| 144 | raise |
| 145 | return HttpResponseForbidden( |
| 146 | ERROR_PAGE_TEMPLATE % {"title": "403 Forbidden", "details": ""}, |
| 147 | ) |
| 148 | return HttpResponseForbidden( |
| 149 | template.render(request=request, context={"exception": str(exception)}) |
| 150 | ) |