views.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, 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 one variable to the template: ``request_path``,
  54. which is the URL that resulted in the error.
  55. Three things to note about 404 views:
  56. * The 404 view is also called if Django doesn't find a match after
  57. checking every regular expression in the URLconf.
  58. * The 404 view is passed a :class:`~django.template.RequestContext` and
  59. will have access to variables supplied by your template context
  60. processors (e.g. ``MEDIA_URL``).
  61. * If :setting:`DEBUG` is set to ``True`` (in your settings module), then
  62. your 404 view will never be used, and your URLconf will be displayed
  63. instead, with some debug information.
  64. .. _http_internal_server_error_view:
  65. The 500 (server error) view
  66. ---------------------------
  67. .. function:: defaults.server_error(request, template_name='500.html')
  68. Similarly, Django executes special-case behavior in the case of runtime errors
  69. in view code. If a view results in an exception, Django will, by default, call
  70. the view ``django.views.defaults.server_error``, which either produces a very
  71. simple "Server Error" message or loads and renders the template ``500.html`` if
  72. you created it in your root template directory.
  73. The default 500 view passes no variables to the ``500.html`` template and is
  74. rendered with an empty ``Context`` to lessen the chance of additional errors.
  75. If :setting:`DEBUG` is set to ``True`` (in your settings module), then
  76. your 500 view will never be used, and the traceback will be displayed
  77. instead, with some debug information.
  78. .. _http_forbidden_view:
  79. The 403 (HTTP Forbidden) view
  80. -----------------------------
  81. .. function:: defaults.permission_denied(request, template_name='403.html')
  82. In the same vein as the 404 and 500 views, Django has a view to handle 403
  83. Forbidden errors. If a view results in a 403 exception then Django will, by
  84. default, call the view ``django.views.defaults.permission_denied``.
  85. This view loads and renders the template ``403.html`` in your root template
  86. directory, or if this file does not exist, instead serves the text
  87. "403 Forbidden", as per :rfc:`2616` (the HTTP 1.1 Specification).
  88. ``django.views.defaults.permission_denied`` is triggered by a
  89. :exc:`~django.core.exceptions.PermissionDenied` exception. To deny access in a
  90. view you can use code like this::
  91. from django.core.exceptions import PermissionDenied
  92. def edit(request, pk):
  93. if not request.user.is_staff:
  94. raise PermissionDenied
  95. # ...
  96. .. _http_bad_request_view:
  97. The 400 (bad request) view
  98. --------------------------
  99. .. function:: defaults.bad_request(request, template_name='400.html')
  100. When a :exc:`~django.core.exceptions.SuspiciousOperation` is raised in Django,
  101. it may be handled by a component of Django (for example resetting the session
  102. data). If not specifically handled, Django will consider the current request a
  103. 'bad request' instead of a server error.
  104. ``django.views.defaults.bad_request``, is otherwise very similar to the
  105. ``server_error`` view, but returns with the status code 400 indicating that
  106. the error condition was the result of a client operation.
  107. ``bad_request`` views are also only used when :setting:`DEBUG` is ``False``.