| 1 |
from urllib.parse import quote |
| 2 |
|
| 3 |
from django.http import ( |
| 4 |
HttpResponseBadRequest, |
| 5 |
HttpResponseForbidden, |
| 6 |
HttpResponseNotFound, |
| 7 |
HttpResponseServerError, |
| 8 |
) |
| 9 |
from django.template import Context, Engine, TemplateDoesNotExist, loader |
| 10 |
from django.views.decorators.csrf import requires_csrf_token |
| 11 |
|
| 12 |
ERROR_404_TEMPLATE_NAME = "404.html" |
| 13 |
ERROR_403_TEMPLATE_NAME = "403.html" |
| 14 |
ERROR_400_TEMPLATE_NAME = "400.html" |
| 15 |
ERROR_500_TEMPLATE_NAME = "500.html" |
| 16 |
ERROR_PAGE_TEMPLATE = """ |
| 17 |
<!doctype html> |
| 18 |
<html lang="en"> |
| 19 |
<head> |
| 20 |
<title>%(title)s</title> |
| 21 |
</head> |
| 22 |
<body> |
| 23 |
<h1>%(title)s</h1><p>%(details)s</p> |
| 24 |
</body> |
| 25 |
</html> |
| 26 |
""" |
| 27 |
|
| 28 |
|
| 29 |
# These views can be called when CsrfViewMiddleware.process_view() not run, |
| 30 |
# therefore need @requires_csrf_token in case the template needs |
| 31 |
# {% csrf_token %}. |
| 32 |
|
| 33 |
|
| 34 |
@requires_csrf_token |
| 35 |
def page_not_found(request, exception, template_name=ERROR_404_TEMPLATE_NAME): |
| 36 |
""" |
| 37 |
Default 404 handler. |
| 38 |
|
| 39 |
Templates: :template:`404.html` |
| 40 |
Context: |
| 41 |
request_path |
| 42 |
The path of the requested URL (e.g., '/app/pages/bad_page/'). It's |
| 43 |
quoted to prevent a content injection attack. |
| 44 |
exception |
| 45 |
The message from the exception which triggered the 404 (if one was |
| 46 |
supplied), or the exception class name |
| 47 |
""" |
| 48 |
exception_repr = exception.__class__.__name__ |
| 49 |
# Try to get an "interesting" exception message, if any (and not the ugly |
| 50 |
# Resolver404 dictionary) |
| 51 |
try: |
| 52 |
message = exception.args[0] |
| 53 |
except (AttributeError, IndexError): |
| 54 |
pass |
| 55 |
else: |
| 56 |
if isinstance(message, str): |
| 57 |
exception_repr = message |
| 58 |
context = { |
| 59 |
"request_path": quote(request.path), |
| 60 |
"exception": exception_repr, |
| 61 |
} |
| 62 |
try: |
| 63 |
template = loader.get_template(template_name) |
| 64 |
body = template.render(context, request) |
| 65 |
except TemplateDoesNotExist: |
| 66 |
if template_name != ERROR_404_TEMPLATE_NAME: |
| 67 |
# Reraise if it's a missing custom template. |
| 68 |
raise |
| 69 |
# Render template (even though there are no substitutions) to allow |
| 70 |
# inspecting the context in tests. |
| 71 |
template = Engine().from_string( |
| 72 |
ERROR_PAGE_TEMPLATE |
| 73 |
% { |
| 74 |
"title": "Not Found", |
| 75 |
"details": "The requested resource was not found on this server.", |
| 76 |
}, |
| 77 |
) |
| 78 |
body = template.render(Context(context)) |
| 79 |
return HttpResponseNotFound(body) |
| 80 |
|
| 81 |
|
| 82 |
@requires_csrf_token |
| 83 |
def server_error(request, template_name=ERROR_500_TEMPLATE_NAME): |
| 84 |
""" |
| 85 |
500 error handler. |
| 86 |
|
| 87 |
Templates: :template:`500.html` |
| 88 |
Context: None |
| 89 |
""" |
| 90 |
try: |
| 91 |
template = loader.get_template(template_name) |
| 92 |
except TemplateDoesNotExist: |
| 93 |
if template_name != ERROR_500_TEMPLATE_NAME: |
| 94 |
# Reraise if it's a missing custom template. |
| 95 |
raise |
| 96 |
return HttpResponseServerError( |
| 97 |
ERROR_PAGE_TEMPLATE % {"title": "Server Error (500)", "details": ""}, |
| 98 |
) |
| 99 |
return HttpResponseServerError(template.render()) |
| 100 |
|
| 101 |
|
| 102 |
@requires_csrf_token |
| 103 |
def bad_request(request, exception, template_name=ERROR_400_TEMPLATE_NAME): |
| 104 |
""" |
| 105 |
400 error handler. |
| 106 |
|
| 107 |
Templates: :template:`400.html` |
| 108 |
Context: None |
| 109 |
""" |
| 110 |
try: |
| 111 |
template = loader.get_template(template_name) |
| 112 |
body = template.render(request=request) |
| 113 |
except TemplateDoesNotExist: |
| 114 |
if template_name != ERROR_400_TEMPLATE_NAME: |
| 115 |
# Reraise if it's a missing custom template. |
| 116 |
raise |
| 117 |
return HttpResponseBadRequest( |
| 118 |
ERROR_PAGE_TEMPLATE % {"title": "Bad Request (400)", "details": ""}, |
| 119 |
) |
| 120 |
# No exception content is passed to the template, to not disclose any |
| 121 |
# sensitive information. |
| 122 |
return HttpResponseBadRequest(body) |
| 123 |
|
| 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 |
) |