/lib/python/django/views/defaults.py

https://github.com/mozilla/moztrap-vendor-lib · Python · 91 lines · 79 code · 3 blank · 9 comment · 0 complexity · 68f0f80a2ead52c771ac7c142f86cbff MD5 · raw file

  1. import warnings
  2. from django import http
  3. from django.template import (Context, RequestContext,
  4. loader, Template, TemplateDoesNotExist)
  5. from django.views.decorators.csrf import requires_csrf_token
  6. # This can be called when CsrfViewMiddleware.process_view has not run,
  7. # therefore need @requires_csrf_token in case the template needs
  8. # {% csrf_token %}.
  9. @requires_csrf_token
  10. def page_not_found(request, template_name='404.html'):
  11. """
  12. Default 404 handler.
  13. Templates: :template:`404.html`
  14. Context:
  15. request_path
  16. The path of the requested URL (e.g., '/app/pages/bad_page/')
  17. """
  18. try:
  19. template = loader.get_template(template_name)
  20. content_type = None # Django will use DEFAULT_CONTENT_TYPE
  21. except TemplateDoesNotExist:
  22. template = Template(
  23. '<h1>Not Found</h1>'
  24. '<p>The requested URL {{ request_path }} was not found on this server.</p>')
  25. content_type = 'text/html'
  26. body = template.render(RequestContext(request, {'request_path': request.path}))
  27. return http.HttpResponseNotFound(body, content_type=content_type)
  28. @requires_csrf_token
  29. def server_error(request, template_name='500.html'):
  30. """
  31. 500 error handler.
  32. Templates: :template:`500.html`
  33. Context: None
  34. """
  35. try:
  36. template = loader.get_template(template_name)
  37. except TemplateDoesNotExist:
  38. return http.HttpResponseServerError('<h1>Server Error (500)</h1>', content_type='text/html')
  39. return http.HttpResponseServerError(template.render(Context({})))
  40. @requires_csrf_token
  41. def bad_request(request, template_name='400.html'):
  42. """
  43. 400 error handler.
  44. Templates: :template:`400.html`
  45. Context: None
  46. """
  47. try:
  48. template = loader.get_template(template_name)
  49. except TemplateDoesNotExist:
  50. return http.HttpResponseBadRequest('<h1>Bad Request (400)</h1>', content_type='text/html')
  51. return http.HttpResponseBadRequest(template.render(Context({})))
  52. # This can be called when CsrfViewMiddleware.process_view has not run,
  53. # therefore need @requires_csrf_token in case the template needs
  54. # {% csrf_token %}.
  55. @requires_csrf_token
  56. def permission_denied(request, template_name='403.html'):
  57. """
  58. Permission denied (403) handler.
  59. Templates: :template:`403.html`
  60. Context: None
  61. If the template does not exist, an Http403 response containing the text
  62. "403 Forbidden" (as per RFC 2616) will be returned.
  63. """
  64. try:
  65. template = loader.get_template(template_name)
  66. except TemplateDoesNotExist:
  67. return http.HttpResponseForbidden('<h1>403 Forbidden</h1>', content_type='text/html')
  68. return http.HttpResponseForbidden(template.render(RequestContext(request)))
  69. def shortcut(request, content_type_id, object_id):
  70. warnings.warn(
  71. "django.views.defaults.shortcut will be removed in Django 1.8. "
  72. "Import it from django.contrib.contenttypes.views instead.",
  73. PendingDeprecationWarning, stacklevel=2)
  74. from django.contrib.contenttypes.views import shortcut as real_shortcut
  75. return real_shortcut(request, content_type_id, object_id)