PageRenderTime 84ms CodeModel.GetById 42ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 0ms

/docs/topics/http/views.txt

https://code.google.com/p/mango-py/
Plain Text | 202 lines | 149 code | 53 blank | 0 comment | 0 complexity | 604c36cd43426fcb8dbfc403a9ba7b5c MD5 | raw file
  1=============
  2Writing views
  3=============
  4
  5A view function, or *view* for short, is simply a Python function that takes a
  6Web request and returns a Web response. This response can be the HTML contents
  7of 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
  9necessary to return that response. This code can live anywhere you want, as long
 10as it's on your Python path. There's no other requirement--no "magic," so to
 11speak. For the sake of putting the code *somewhere*, the convention is to
 12put views in a file called ``views.py``, placed in your project or
 13application directory.
 14
 15A simple view
 16=============
 17
 18Here's a view that returns the current date and time, as an HTML document:
 19
 20.. code-block:: python
 21
 22    from django.http import HttpResponse
 23    import datetime
 24
 25    def current_datetime(request):
 26        now = datetime.datetime.now()
 27        html = "<html><body>It is now %s.</body></html>" % now
 28        return HttpResponse(html)
 29
 30Let's step through this code one line at a time:
 31
 32    * First, we import the class :class:`~django.http.HttpResponse` from the
 33      :mod:`django.http` module, along with Python's ``datetime`` library.
 34
 35    * Next, we define a function called ``current_datetime``. This is the view
 36      function. Each view function takes an :class:`~django.http.HttpRequest`
 37      object as its first parameter, which is typically named ``request``.
 38
 39      Note that the name of the view function doesn't matter; it doesn't have to
 40      be named in a certain way in order for Django to recognize it. We're
 41      calling it ``current_datetime`` here, because that name clearly indicates
 42      what it does.
 43
 44    * The view returns an :class:`~django.http.HttpResponse` object that
 45      contains the generated response. Each view function is responsible for
 46      returning an :class:`~django.http.HttpResponse` object. (There are
 47      exceptions, but we'll get to those later.)
 48
 49.. admonition:: Django's Time Zone
 50
 51    Django includes a :setting:`TIME_ZONE` setting that defaults to
 52    ``America/Chicago``. This probably isn't where you live, so you might want
 53    to change it in your settings file.
 54
 55Mapping URLs to views
 56=====================
 57
 58So, to recap, this view function returns an HTML page that includes the current
 59date and time. To display this view at a particular URL, you'll need to create a
 60*URLconf*; see :doc:`/topics/http/urls` for instructions.
 61
 62Returning errors
 63================
 64
 65Returning HTTP error codes in Django is easy. There are subclasses of
 66:class:`~django.http.HttpResponse` for a number of common HTTP status codes
 67other than 200 (which means *"OK"*). You can find the full list of available
 68subclasses in the :ref:`request/response <ref-httpresponse-subclasses>`
 69documentation.  Just return an instance of one of those subclasses instead of
 70a normal :class:`~django.http.HttpResponse` in order to signify an error. For
 71example::
 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
 80There isn't a specialized subclass for every possible HTTP response code,
 81since many of them aren't going to be that common. However, as documented in
 82the :class:`~django.http.HttpResponse` documentation, you can also pass the
 83HTTP status code into the constructor for :class:`~django.http.HttpResponse`
 84to create a return class for any status code you like. For example::
 85
 86    def my_view(request):
 87        # ...
 88
 89        # Return a "created" (201) response code.
 90        return HttpResponse(status=201)
 91
 92Because 404 errors are by far the most common HTTP error, there's an easier way
 93to handle those errors.
 94
 95The Http404 exception
 96---------------------
 97
 98.. class:: django.http.Http404()
 99
100When you return an error such as :class:`~django.http.HttpResponseNotFound`,
101you're responsible for defining the HTML of the resulting error page::
102
103    return HttpResponseNotFound('<h1>Page not found</h1>')
104
105For convenience, and because it's a good idea to have a consistent 404 error page
106across your site, Django provides an ``Http404`` exception. If you raise
107``Http404`` at any point in a view function, Django will catch it and return the
108standard error page for your application, along with an HTTP error code 404.
109
110Example usage::
111
112    from django.http import Http404
113
114    def detail(request, poll_id):
115        try:
116            p = Poll.objects.get(pk=poll_id)
117        except Poll.DoesNotExist:
118            raise Http404
119        return render_to_response('polls/detail.html', {'poll': p})
120
121In order to use the ``Http404`` exception to its fullest, you should create a
122template that is displayed when a 404 error is raised. This template should be
123called ``404.html`` and located in the top level of your template tree.
124
125Customizing error views
126=======================
127
128The 404 (page not found) view
129-----------------------------
130
131When you raise an ``Http404`` exception, Django loads a special view devoted
132to handling 404 errors. By default, it's the view
133``django.views.defaults.page_not_found``, which loads and renders the template
134``404.html``.
135
136This means you need to define a ``404.html`` template in your root template
137directory. This template will be used for all 404 errors.
138
139This ``page_not_found`` view should suffice for 99% of Web applications, but if
140you want to override the 404 view, you can specify ``handler404`` in your
141URLconf, like so::
142
143    handler404 = 'mysite.views.my_custom_404_view'
144
145Behind the scenes, Django determines the 404 view by looking for ``handler404``.
146By default, URLconfs contain the following line::
147
148    from django.conf.urls.defaults import *
149
150That takes care of setting ``handler404`` in the current module. As you can see
151in ``django/conf/urls/defaults.py``, ``handler404`` is set to
152``'django.views.defaults.page_not_found'`` by default.
153
154Three things to note about 404 views:
155
156    * The 404 view is also called if Django doesn't find a match after checking
157      every regular expression in the URLconf.
158
159    * If you don't define your own 404 view -- and simply use the
160      default, which is recommended -- you still have one obligation:
161      you must create a ``404.html`` template in the root of your
162      template directory. The default 404 view will use that template
163      for all 404 errors. The default 404 view will pass one variable
164      to the template: ``request_path``, which is the URL that resulted
165      in the 404.
166
167    * The 404 view is passed a :class:`~django.template.RequestContext` and
168      will have access to variables supplied by your
169      :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting (e.g.,
170      :setting:`MEDIA_URL`).
171
172    * If :setting:`DEBUG` is set to ``True`` (in your settings module), then
173      your 404 view will never be used, and the traceback will be displayed
174      instead.
175
176The 500 (server error) view
177----------------------------
178
179Similarly, Django executes special-case behavior in the case of runtime errors
180in view code. If a view results in an exception, Django will, by default, call
181the view ``django.views.defaults.server_error``, which loads and renders the
182template ``500.html``.
183
184This means you need to define a ``500.html`` template in your root template
185directory. This template will be used for all server errors. The default 500
186view passes no variables to this template and is rendered with an empty
187``Context`` to lessen the chance of additional errors.
188
189This ``server_error`` view should suffice for 99% of Web applications, but if
190you want to override the view, you can specify ``handler500`` in your
191URLconf, like so::
192
193    handler500 = 'mysite.views.my_custom_error_view'
194
195Behind the scenes, Django determines the error view by looking for ``handler500``.
196By default, URLconfs contain the following line::
197
198    from django.conf.urls.defaults import *
199
200That takes care of setting ``handler500`` in the current module. As you can see
201in ``django/conf/urls/defaults.py``, ``handler500`` is set to
202``'django.views.defaults.server_error'`` by default.