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