defaults.py 4.8 KB

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