views.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. =============
  2. Writing views
  3. =============
  4. A view function, or *view* for short, is simply a Python function that takes a
  5. Web request and returns a Web response. This response can be the HTML contents
  6. of a Web page, or a redirect, or a 404 error, or an XML document, or an image .
  7. . . or anything, really. The view itself contains whatever arbitrary logic is
  8. necessary to return that response. This code can live anywhere you want, as long
  9. as it's on your Python path. There's no other requirement--no "magic," so to
  10. speak. For the sake of putting the code *somewhere*, the convention is to
  11. put views in a file called ``views.py``, placed in your project or
  12. application directory.
  13. A simple view
  14. =============
  15. Here's a view that returns the current date and time, as an HTML document:
  16. .. code-block:: python
  17. from django.http import HttpResponse
  18. import datetime
  19. def current_datetime(request):
  20. now = datetime.datetime.now()
  21. html = "<html><body>It is now %s.</body></html>" % now
  22. return HttpResponse(html)
  23. Let's step through this code one line at a time:
  24. * First, we import the class :class:`~django.http.HttpResponse` from the
  25. :mod:`django.http` module, along with Python's ``datetime`` library.
  26. * Next, we define a function called ``current_datetime``. This is the view
  27. function. Each view function takes an :class:`~django.http.HttpRequest`
  28. object as its first parameter, which is typically named ``request``.
  29. Note that the name of the view function doesn't matter; it doesn't have to
  30. be named in a certain way in order for Django to recognize it. We're
  31. calling it ``current_datetime`` here, because that name clearly indicates
  32. what it does.
  33. * The view returns an :class:`~django.http.HttpResponse` object that
  34. contains the generated response. Each view function is responsible for
  35. returning an :class:`~django.http.HttpResponse` object. (There are
  36. exceptions, but we'll get to those later.)
  37. .. admonition:: Django's Time Zone
  38. Django includes a :setting:`TIME_ZONE` setting that defaults to
  39. ``America/Chicago``. This probably isn't where you live, so you might want
  40. to change it in your settings file.
  41. Mapping URLs to views
  42. =====================
  43. So, to recap, this view function returns an HTML page that includes the current
  44. date and time. To display this view at a particular URL, you'll need to create a
  45. *URLconf*; see :doc:`/topics/http/urls` for instructions.
  46. Returning errors
  47. ================
  48. Returning HTTP error codes in Django is easy. There are subclasses of
  49. :class:`~django.http.HttpResponse` for a number of common HTTP status codes
  50. other than 200 (which means *"OK"*). You can find the full list of available
  51. subclasses in the :ref:`request/response <ref-httpresponse-subclasses>`
  52. documentation. Just return an instance of one of those subclasses instead of
  53. a normal :class:`~django.http.HttpResponse` in order to signify an error. For
  54. example::
  55. from django.http import HttpResponse, HttpResponseNotFound
  56. def my_view(request):
  57. # ...
  58. if foo:
  59. return HttpResponseNotFound('<h1>Page not found</h1>')
  60. else:
  61. return HttpResponse('<h1>Page was found</h1>')
  62. There isn't a specialized subclass for every possible HTTP response code,
  63. since many of them aren't going to be that common. However, as documented in
  64. the :class:`~django.http.HttpResponse` documentation, you can also pass the
  65. HTTP status code into the constructor for :class:`~django.http.HttpResponse`
  66. to create a return class for any status code you like. For example::
  67. from django.http import HttpResponse
  68. def my_view(request):
  69. # ...
  70. # Return a "created" (201) response code.
  71. return HttpResponse(status=201)
  72. Because 404 errors are by far the most common HTTP error, there's an easier way
  73. to handle those errors.
  74. The Http404 exception
  75. ---------------------
  76. .. class:: django.http.Http404()
  77. When you return an error such as :class:`~django.http.HttpResponseNotFound`,
  78. you're responsible for defining the HTML of the resulting error page::
  79. return HttpResponseNotFound('<h1>Page not found</h1>')
  80. For convenience, and because it's a good idea to have a consistent 404 error page
  81. across your site, Django provides an ``Http404`` exception. If you raise
  82. ``Http404`` at any point in a view function, Django will catch it and return the
  83. standard error page for your application, along with an HTTP error code 404.
  84. Example usage::
  85. from django.http import Http404
  86. from django.shortcuts import render_to_response
  87. from polls.models import Poll
  88. def detail(request, poll_id):
  89. try:
  90. p = Poll.objects.get(pk=poll_id)
  91. except Poll.DoesNotExist:
  92. raise Http404
  93. return render_to_response('polls/detail.html', {'poll': p})
  94. In order to use the ``Http404`` exception to its fullest, you should create a
  95. template that is displayed when a 404 error is raised. This template should be
  96. called ``404.html`` and located in the top level of your template tree.
  97. .. _customizing-error-views:
  98. Customizing error views
  99. =======================
  100. .. _http_not_found_view:
  101. The 404 (page not found) view
  102. -----------------------------
  103. .. function:: django.views.defaults.page_not_found(request, template_name='404.html')
  104. When you raise :exc:`~django.http.Http404` from within a view, Django loads a
  105. special view devoted to handling 404 errors. By default, it's the view
  106. :func:`django.views.defaults.page_not_found`, which either produces a very
  107. simple "Not Found" message or loads and renders the template ``404.html`` if
  108. you created it in your root template directory.
  109. The default 404 view will pass one variable to the template: ``request_path``,
  110. which is the URL that resulted in the error.
  111. The ``page_not_found`` view should suffice for 99% of Web applications, but if
  112. you want to override it, you can specify :data:`~django.conf.urls.handler404`
  113. in your root URLconf (setting ``handler404`` anywhere else will have no
  114. effect), like so::
  115. handler404 = 'mysite.views.my_custom_404_view'
  116. Behind the scenes, Django determines the 404 view by looking for
  117. ``handler404`` in your root URLconf, and falling back to
  118. ``django.views.defaults.page_not_found`` if you did not define one.
  119. Three things to note about 404 views:
  120. * The 404 view is also called if Django doesn't find a match after
  121. checking every regular expression in the URLconf.
  122. * The 404 view is passed a :class:`~django.template.RequestContext` and
  123. will have access to variables supplied by your
  124. :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting (e.g., ``MEDIA_URL``).
  125. * If :setting:`DEBUG` is set to ``True`` (in your settings module), then
  126. your 404 view will never be used, and your URLconf will be displayed
  127. instead, with some debug information.
  128. .. _http_internal_server_error_view:
  129. The 500 (server error) view
  130. ----------------------------
  131. .. function:: django.views.defaults.server_error(request, template_name='500.html')
  132. Similarly, Django executes special-case behavior in the case of runtime errors
  133. in view code. If a view results in an exception, Django will, by default, call
  134. the view ``django.views.defaults.server_error``, which either produces a very
  135. simple "Server Error" message or loads and renders the template ``500.html`` if
  136. you created it in your root template directory.
  137. The default 500 view passes no variables to the ``500.html`` template and is
  138. rendered with an empty ``Context`` to lessen the chance of additional errors.
  139. This ``server_error`` view should suffice for 99% of Web applications, but if
  140. you want to override the view, you can specify
  141. :data:`~django.conf.urls.handler500` in your root URLconf, like so::
  142. handler500 = 'mysite.views.my_custom_error_view'
  143. Behind the scenes, Django determines the 500 view by looking for
  144. ``handler500`` in your root URLconf, and falling back to
  145. ``django.views.defaults.server_error`` if you did not define one.
  146. If :setting:`DEBUG` is set to ``True`` (in your settings module), then
  147. your 500 view will never be used, and the traceback will be displayed
  148. instead, with some debug information.
  149. .. _http_forbidden_view:
  150. The 403 (HTTP Forbidden) view
  151. -----------------------------
  152. .. function:: django.views.defaults.permission_denied(request, template_name='403.html')
  153. In the same vein as the 404 and 500 views, Django has a view to handle 403
  154. Forbidden errors. If a view results in a 403 exception then Django will, by
  155. default, call the view ``django.views.defaults.permission_denied``.
  156. This view loads and renders the template ``403.html`` in your root template
  157. directory, or if this file does not exist, instead serves the text
  158. "403 Forbidden", as per :rfc:`2616` (the HTTP 1.1 Specification).
  159. ``django.views.defaults.permission_denied`` is triggered by a
  160. :exc:`~django.core.exceptions.PermissionDenied` exception. To deny access in a
  161. view you can use code like this::
  162. from django.core.exceptions import PermissionDenied
  163. def edit(request, pk):
  164. if not request.user.is_staff:
  165. raise PermissionDenied
  166. # ...
  167. It is possible to override ``django.views.defaults.permission_denied`` in the
  168. same way you can for the 404 and 500 views by specifying a
  169. :data:`~django.conf.urls.handler403` in your root URLconf::
  170. handler403 = 'mysite.views.my_custom_permission_denied_view'
  171. .. _http_bad_request_view:
  172. The 400 (bad request) view
  173. --------------------------
  174. .. function:: django.views.defaults.bad_request(request, template_name='400.html')
  175. When a :exc:`~django.core.exceptions.SuspiciousOperation` is raised in Django,
  176. it may be handled by a component of Django (for example resetting the session
  177. data). If not specifically handled, Django will consider the current request a
  178. 'bad request' instead of a server error.
  179. ``django.views.defaults.bad_request``, is otherwise very similar to the
  180. ``server_error`` view, but returns with the status code 400 indicating that
  181. the error condition was the result of a client operation.
  182. Like ``server_error``, the default ``bad_request`` should suffice for
  183. 99% of Web applications, but if you want to override the view, you can specify
  184. :data:`~django.conf.urls.handler400` in your root URLconf, like so::
  185. handler400 = 'mysite.views.my_custom_bad_request_view'
  186. ``bad_request`` views are also only used when :setting:`DEBUG` is ``False``.