2
0

views.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ==============
  2. Built-in Views
  3. ==============
  4. .. module:: django.views
  5. :synopsis: Django's built-in views.
  6. Several of Django's built-in views are documented in
  7. :doc:`/topics/http/views` as well as elsewhere in the documentation.
  8. Serving files in development
  9. ============================
  10. .. function:: static.serve(request, path, document_root, show_indexes=False)
  11. There may be files other than your project's static assets that, for
  12. convenience, you'd like to have Django serve for you in local development.
  13. The :func:`~django.views.static.serve` view can be used to serve any directory
  14. you give it. (This view is **not** hardened for production use and should be
  15. used only as a development aid; you should serve these files in production
  16. using a real front-end web server).
  17. The most likely example is user-uploaded content in :setting:`MEDIA_ROOT`.
  18. ``django.contrib.staticfiles`` is intended for static assets and has no
  19. built-in handling for user-uploaded files, but you can have Django serve your
  20. :setting:`MEDIA_ROOT` by appending something like this to your URLconf::
  21. from django.conf import settings
  22. from django.views.static import serve
  23. # ... the rest of your URLconf goes here ...
  24. if settings.DEBUG:
  25. urlpatterns += [
  26. url(r'^media/(?P<path>.*)$', serve, {
  27. 'document_root': settings.MEDIA_ROOT,
  28. }),
  29. ]
  30. Note, the snippet assumes your :setting:`MEDIA_URL` has a value of
  31. ``'/media/'``. This will call the :func:`~django.views.static.serve` view,
  32. passing in the path from the URLconf and the (required) ``document_root``
  33. parameter.
  34. Since it can become a bit cumbersome to define this URL pattern, Django
  35. ships with a small URL helper function :func:`~django.conf.urls.static.static`
  36. that takes as parameters the prefix such as :setting:`MEDIA_URL` and a dotted
  37. path to a view, such as ``'django.views.static.serve'``. Any other function
  38. parameter will be transparently passed to the view.
  39. .. _error-views:
  40. Error views
  41. ===========
  42. Django comes with a few views by default for handling HTTP errors. To override
  43. these with your own custom views, see :ref:`customizing-error-views`.
  44. .. _http_not_found_view:
  45. The 404 (page not found) view
  46. -----------------------------
  47. .. function:: defaults.page_not_found(request, exception, template_name='404.html')
  48. When you raise :exc:`~django.http.Http404` from within a view, Django loads a
  49. special view devoted to handling 404 errors. By default, it's the view
  50. :func:`django.views.defaults.page_not_found`, which either produces a very
  51. simple "Not Found" message or loads and renders the template ``404.html`` if
  52. you created it in your root template directory.
  53. The default 404 view will pass two variables to the template: ``request_path``,
  54. which is the URL that resulted in the error, and ``exception``, which is a
  55. useful representation of the exception that triggered the view (e.g. containing
  56. any message passed to a specific ``Http404`` instance).
  57. Three things to note about 404 views:
  58. * The 404 view is also called if Django doesn't find a match after
  59. checking every regular expression in the URLconf.
  60. * The 404 view is passed a :class:`~django.template.RequestContext` and
  61. will have access to variables supplied by your template context
  62. processors (e.g. ``MEDIA_URL``).
  63. * If :setting:`DEBUG` is set to ``True`` (in your settings module), then
  64. your 404 view will never be used, and your URLconf will be displayed
  65. instead, with some debug information.
  66. .. versionchanged:: 1.9
  67. The signature of ``page_not_found()`` changed. The function now accepts a
  68. second parameter, the exception that triggered the error. A useful
  69. representation of the exception is also passed in the template context.
  70. .. _http_internal_server_error_view:
  71. The 500 (server error) view
  72. ---------------------------
  73. .. function:: defaults.server_error(request, template_name='500.html')
  74. Similarly, Django executes special-case behavior in the case of runtime errors
  75. in view code. If a view results in an exception, Django will, by default, call
  76. the view ``django.views.defaults.server_error``, which either produces a very
  77. simple "Server Error" message or loads and renders the template ``500.html`` if
  78. you created it in your root template directory.
  79. The default 500 view passes no variables to the ``500.html`` template and is
  80. rendered with an empty ``Context`` to lessen the chance of additional errors.
  81. If :setting:`DEBUG` is set to ``True`` (in your settings module), then
  82. your 500 view will never be used, and the traceback will be displayed
  83. instead, with some debug information.
  84. .. _http_forbidden_view:
  85. The 403 (HTTP Forbidden) view
  86. -----------------------------
  87. .. function:: defaults.permission_denied(request, exception, template_name='403.html')
  88. In the same vein as the 404 and 500 views, Django has a view to handle 403
  89. Forbidden errors. If a view results in a 403 exception then Django will, by
  90. default, call the view ``django.views.defaults.permission_denied``.
  91. This view loads and renders the template ``403.html`` in your root template
  92. directory, or if this file does not exist, instead serves the text
  93. "403 Forbidden", as per :rfc:`7231#section-6.5.3` (the HTTP 1.1 Specification).
  94. The template context contains ``exception``, which is the unicode
  95. representation of the exception that triggered the view.
  96. ``django.views.defaults.permission_denied`` is triggered by a
  97. :exc:`~django.core.exceptions.PermissionDenied` exception. To deny access in a
  98. view you can use code like this::
  99. from django.core.exceptions import PermissionDenied
  100. def edit(request, pk):
  101. if not request.user.is_staff:
  102. raise PermissionDenied
  103. # ...
  104. .. versionchanged:: 1.9
  105. The signature of ``permission_denied()`` changed in Django 1.9. The function
  106. now accepts a second parameter, the exception that triggered the error. The
  107. unicode representation of the exception is also passed in the template
  108. context.
  109. .. _http_bad_request_view:
  110. The 400 (bad request) view
  111. --------------------------
  112. .. function:: defaults.bad_request(request, exception, template_name='400.html')
  113. When a :exc:`~django.core.exceptions.SuspiciousOperation` is raised in Django,
  114. it may be handled by a component of Django (for example resetting the session
  115. data). If not specifically handled, Django will consider the current request a
  116. 'bad request' instead of a server error.
  117. ``django.views.defaults.bad_request``, is otherwise very similar to the
  118. ``server_error`` view, but returns with the status code 400 indicating that
  119. the error condition was the result of a client operation. By default, nothing
  120. related to the exception that triggered the view is passed to the template
  121. context, as the exception message might contain sensitive information like
  122. filesystem paths.
  123. ``bad_request`` views are also only used when :setting:`DEBUG` is ``False``.
  124. .. versionchanged:: 1.9
  125. The signature of ``bad_request()`` changed in Django 1.9. The function
  126. now accepts a second parameter, the exception that triggered the error.