Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

defaults.py 4.2KB

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