defaults.py 3.2 KB

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