views.txt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. =============
  2. Writing views
  3. =============
  4. A view function, or *view* for short, is 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. from django.http import HttpResponse
  17. import datetime
  18. def current_datetime(request):
  19. now = datetime.datetime.now()
  20. html = '<html lang="en"><body>It is now %s.</body></html>' % now
  21. return HttpResponse(html)
  22. Let's step through this code one line at a time:
  23. * First, we import the class :class:`~django.http.HttpResponse` from the
  24. :mod:`django.http` module, along with Python's ``datetime`` library.
  25. * Next, we define a function called ``current_datetime``. This is the view
  26. function. Each view function takes an :class:`~django.http.HttpRequest`
  27. object as its first parameter, which is typically named ``request``.
  28. Note that the name of the view function doesn't matter; it doesn't have to
  29. be named in a certain way in order for Django to recognize it. We're
  30. calling it ``current_datetime`` here, because that name clearly indicates
  31. what it does.
  32. * The view returns an :class:`~django.http.HttpResponse` object that
  33. contains the generated response. Each view function is responsible for
  34. returning an :class:`~django.http.HttpResponse` object. (There are
  35. exceptions, but we'll get to those later.)
  36. .. admonition:: Django's Time Zone
  37. Django includes a :setting:`TIME_ZONE` setting that defaults to
  38. ``America/Chicago``. This probably isn't where you live, so you might want
  39. to change it in your settings file.
  40. Mapping URLs to views
  41. =====================
  42. So, to recap, this view function returns an HTML page that includes the current
  43. date and time. To display this view at a particular URL, you'll need to create a
  44. *URLconf*; see :doc:`/topics/http/urls` for instructions.
  45. Returning errors
  46. ================
  47. Django provides help for returning HTTP error codes. There are subclasses of
  48. :class:`~django.http.HttpResponse` for a number of common HTTP status codes
  49. other than 200 (which means *"OK"*). You can find the full list of available
  50. subclasses in the :ref:`request/response <ref-httpresponse-subclasses>`
  51. documentation. Return an instance of one of those subclasses instead of a
  52. normal :class:`~django.http.HttpResponse` in order to signify an error. For
  53. example::
  54. from django.http import HttpResponse, HttpResponseNotFound
  55. def my_view(request):
  56. # ...
  57. if foo:
  58. return HttpResponseNotFound("<h1>Page not found</h1>")
  59. else:
  60. return HttpResponse("<h1>Page was found</h1>")
  61. There isn't a specialized subclass for every possible HTTP response code,
  62. since many of them aren't going to be that common. However, as documented in
  63. the :class:`~django.http.HttpResponse` documentation, you can also pass the
  64. HTTP status code into the constructor for :class:`~django.http.HttpResponse`
  65. to create a return class for any status code you like. For example::
  66. from django.http import HttpResponse
  67. def my_view(request):
  68. # ...
  69. # Return a "created" (201) response code.
  70. return HttpResponse(status=201)
  71. Because 404 errors are by far the most common HTTP error, there's an easier way
  72. to handle those errors.
  73. The ``Http404`` exception
  74. -------------------------
  75. .. class:: django.http.Http404()
  76. When you return an error such as :class:`~django.http.HttpResponseNotFound`,
  77. you're responsible for defining the HTML of the resulting error page::
  78. return HttpResponseNotFound("<h1>Page not found</h1>")
  79. For convenience, and because it's a good idea to have a consistent 404 error page
  80. across your site, Django provides an ``Http404`` exception. If you raise
  81. ``Http404`` at any point in a view function, Django will catch it and return the
  82. standard error page for your application, along with an HTTP error code 404.
  83. Example usage::
  84. from django.http import Http404
  85. from django.shortcuts import render
  86. from polls.models import Poll
  87. def detail(request, poll_id):
  88. try:
  89. p = Poll.objects.get(pk=poll_id)
  90. except Poll.DoesNotExist:
  91. raise Http404("Poll does not exist")
  92. return render(request, "polls/detail.html", {"poll": p})
  93. In order to show customized HTML when Django returns a 404, you can create an
  94. HTML template named ``404.html`` and place it in the top level of your
  95. template tree. This template will then be served when :setting:`DEBUG` is set
  96. to ``False``.
  97. When :setting:`DEBUG` is ``True``, you can provide a message to ``Http404`` and
  98. it will appear in the standard 404 debug template. Use these messages for
  99. debugging purposes; they generally aren't suitable for use in a production 404
  100. template.
  101. .. _customizing-error-views:
  102. Customizing error views
  103. =======================
  104. The default error views in Django should suffice for most web applications,
  105. but can easily be overridden if you need any custom behavior. Specify the
  106. handlers as seen below in your URLconf (setting them anywhere else will have no
  107. effect).
  108. The :func:`~django.views.defaults.page_not_found` view is overridden by
  109. :data:`~django.conf.urls.handler404`::
  110. handler404 = "mysite.views.my_custom_page_not_found_view"
  111. The :func:`~django.views.defaults.server_error` view is overridden by
  112. :data:`~django.conf.urls.handler500`::
  113. handler500 = "mysite.views.my_custom_error_view"
  114. The :func:`~django.views.defaults.permission_denied` view is overridden by
  115. :data:`~django.conf.urls.handler403`::
  116. handler403 = "mysite.views.my_custom_permission_denied_view"
  117. The :func:`~django.views.defaults.bad_request` view is overridden by
  118. :data:`~django.conf.urls.handler400`::
  119. handler400 = "mysite.views.my_custom_bad_request_view"
  120. .. seealso::
  121. Use the :setting:`CSRF_FAILURE_VIEW` setting to override the CSRF error
  122. view.
  123. Testing custom error views
  124. --------------------------
  125. To test the response of a custom error handler, raise the appropriate exception
  126. in a test view. For example::
  127. from django.core.exceptions import PermissionDenied
  128. from django.http import HttpResponse
  129. from django.test import SimpleTestCase, override_settings
  130. from django.urls import path
  131. def response_error_handler(request, exception=None):
  132. return HttpResponse("Error handler content", status=403)
  133. def permission_denied_view(request):
  134. raise PermissionDenied
  135. urlpatterns = [
  136. path("403/", permission_denied_view),
  137. ]
  138. handler403 = response_error_handler
  139. # ROOT_URLCONF must specify the module that contains handler403 = ...
  140. @override_settings(ROOT_URLCONF=__name__)
  141. class CustomErrorHandlerTests(SimpleTestCase):
  142. def test_handler_renders_template_response(self):
  143. response = self.client.get("/403/")
  144. # Make assertions on the response here. For example:
  145. self.assertContains(response, "Error handler content", status_code=403)
  146. .. _async-views:
  147. Async views
  148. ===========
  149. As well as being synchronous functions, views can also be asynchronous
  150. ("async") functions, normally defined using Python's ``async def`` syntax.
  151. Django will automatically detect these and run them in an async context.
  152. However, you will need to use an async server based on ASGI to get their
  153. performance benefits.
  154. Here's an example of an async view::
  155. import datetime
  156. from django.http import HttpResponse
  157. async def current_datetime(request):
  158. now = datetime.datetime.now()
  159. html = '<html lang="en"><body>It is now %s.</body></html>' % now
  160. return HttpResponse(html)
  161. You can read more about Django's async support, and how to best use async
  162. views, in :doc:`/topics/async`.