views.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. .. _topics-http-views:
  2. =============
  3. Writing Views
  4. =============
  5. A view function, or *view* for short, is simply 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*, let's create a file called
  12. ``views.py`` in the ``mysite`` directory, which you created in the previous
  13. chapter.
  14. A simple view
  15. =============
  16. Here's a view that returns the current date and time, as an HTML document:
  17. .. code-block:: python
  18. from django.http import HttpResponse
  19. import datetime
  20. def current_datetime(request):
  21. now = datetime.datetime.now()
  22. html = "<html><body>It is now %s.</body></html>" % now
  23. return HttpResponse(html)
  24. Let's step through this code one line at a time:
  25. * First, we import the class ``HttpResponse``, which lives in the
  26. ``django.http`` module, along with Python's ``datetime`` library.
  27. * Next, we define a function called ``current_datetime``. This is the view
  28. function. Each view function takes an ``HttpRequest`` object as its first
  29. parameter, which is typically named ``request``.
  30. Note that the name of the view function doesn't matter; it doesn't have to
  31. be named in a certain way in order for Django to recognize it. We're
  32. calling it ``current_datetime`` here, because that name clearly indicates
  33. what it does.
  34. * The view returns an ``HttpResponse`` object that contains the
  35. generated response. Each view function is responsible for returning an
  36. ``HttpResponse`` object. (There are exceptions, but we'll get to those
  37. later.)
  38. .. admonition:: Django's Time Zone
  39. Django includes a ``TIME_ZONE`` setting that defaults to
  40. ``America/Chicago``. This probably isn't where you live, so you might want
  41. to change it in your settings file.
  42. Mapping URLs to Views
  43. =====================
  44. So, to recap, this view function returns an HTML page that includes the current
  45. date and time. To display this view at a particular URL, you'll need to create a
  46. *URLconf*; see :ref:`topics-http-urls` for instructions.
  47. Returning errors
  48. ================
  49. Returning HTTP error codes in Django is easy. There are subclasses of
  50. :class:`~django.http.HttpResponse` for a number of common HTTP status codes
  51. other than 200 (which means *"OK"*). You can find the full list of available
  52. subclasses in the :ref:`request/response <ref-httpresponse-subclasses>`
  53. documentation. Just return an instance of one of those subclasses instead of
  54. a normal :class:`~django.http.HttpResponse` in order to signify an error. For
  55. example::
  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. 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. When you return an error such as ``HttpResponseNotFound``, you're responsible
  76. for defining the HTML of the resulting error page::
  77. return HttpResponseNotFound('<h1>Page not found</h1>')
  78. For convenience, and because it's a good idea to have a consistent 404 error page
  79. across your site, Django provides an ``Http404`` exception. If you raise
  80. ``Http404`` at any point in a view function, Django will catch it and return the
  81. standard error page for your application, along with an HTTP error code 404.
  82. Example usage::
  83. from django.http import Http404
  84. def detail(request, poll_id):
  85. try:
  86. p = Poll.objects.get(pk=poll_id)
  87. except Poll.DoesNotExist:
  88. raise Http404
  89. return render_to_response('polls/detail.html', {'poll': p})
  90. In order to use the ``Http404`` exception to its fullest, you should create a
  91. template that is displayed when a 404 error is raised. This template should be
  92. called ``404.html`` and located in the top level of your template tree.
  93. Customizing error views
  94. =======================
  95. The 404 (page not found) view
  96. -----------------------------
  97. When you raise an ``Http404`` exception, Django loads a special view devoted
  98. to handling 404 errors. By default, it's the view
  99. ``django.views.defaults.page_not_found``, which loads and renders the template
  100. ``404.html``.
  101. This means you need to define a ``404.html`` template in your root template
  102. directory. This template will be used for all 404 errors.
  103. This ``page_not_found`` view should suffice for 99% of Web applications, but if
  104. you want to override the 404 view, you can specify ``handler404`` in your
  105. URLconf, like so::
  106. handler404 = 'mysite.views.my_custom_404_view'
  107. Behind the scenes, Django determines the 404 view by looking for ``handler404``.
  108. By default, URLconfs contain the following line::
  109. from django.conf.urls.defaults import *
  110. That takes care of setting ``handler404`` in the current module. As you can see
  111. in ``django/conf/urls/defaults.py``, ``handler404`` is set to
  112. ``'django.views.defaults.page_not_found'`` by default.
  113. Three things to note about 404 views:
  114. * The 404 view is also called if Django doesn't find a match after checking
  115. every regular expression in the URLconf.
  116. * If you don't define your own 404 view -- and simply use the
  117. default, which is recommended -- you still have one obligation:
  118. you must create a ``404.html`` template in the root of your
  119. template directory. The default 404 view will use that template
  120. for all 404 errors. The default 404 view will pass one variable
  121. to the template: ``request_path``, which is the URL that resulted
  122. in the 404.
  123. * The 404 view is passed a ``RequestContext`` and will have access to
  124. variables supplied by your ``TEMPLATE_CONTEXT_PROCESSORS`` setting (e.g.,
  125. ``MEDIA_URL``).
  126. * If ``DEBUG`` is set to ``True`` (in your settings module), then your 404
  127. view will never be used, and the traceback will be displayed instead.
  128. The 500 (server error) view
  129. ----------------------------
  130. Similarly, Django executes special-case behavior in the case of runtime errors
  131. in view code. If a view results in an exception, Django will, by default, call
  132. the view ``django.views.defaults.server_error``, which loads and renders the
  133. template ``500.html``.
  134. This means you need to define a ``500.html`` template in your root template
  135. directory. This template will be used for all server errors. The default 500
  136. view passes no variables to this template and is rendered with an empty
  137. ``Context`` to lessen the chance of additional errors.
  138. This ``server_error`` view should suffice for 99% of Web applications, but if
  139. you want to override the view, you can specify ``handler500`` in your
  140. URLconf, like so::
  141. handler500 = 'mysite.views.my_custom_error_view'
  142. Behind the scenes, Django determines the error view by looking for ``handler500``.
  143. By default, URLconfs contain the following line::
  144. from django.conf.urls.defaults import *
  145. That takes care of setting ``handler500`` in the current module. As you can see
  146. in ``django/conf/urls/defaults.py``, ``handler500`` is set to
  147. ``'django.views.defaults.server_error'`` by default.