Fix a small typo in docs/upload_handling.txt
[django.git] / docs / generic_views.txt
blobb7beb0b4beba107d9ab65c221745aecb18498936
1 =============
2 Generic views
3 =============
5 Writing Web applications can be monotonous, because we repeat certain patterns
6 again and again. In Django, the most common of these patterns have been
7 abstracted into "generic views" that let you quickly provide common views of
8 an object without actually needing to write any Python code.
10 Django's generic views contain the following:
12     * A set of views for doing list/detail interfaces (for example,
13       Django's `documentation index`_ and `detail pages`_).
15     * A set of views for year/month/day archive pages and associated
16       detail and "latest" pages (for example, the Django weblog's year_,
17       month_, day_, detail_, and latest_ pages).
19     * A set of views for creating, editing, and deleting objects.
21 .. _`documentation index`: http://www.djangoproject.com/documentation/
22 .. _`detail pages`: http://www.djangoproject.com/documentation/faq/
23 .. _year: http://www.djangoproject.com/weblog/2005/
24 .. _month: http://www.djangoproject.com/weblog/2005/jul/
25 .. _day: http://www.djangoproject.com/weblog/2005/jul/20/
26 .. _detail: http://www.djangoproject.com/weblog/2005/jul/20/autoreload/
27 .. _latest: http://www.djangoproject.com/weblog/
29 All of these views are used by creating configuration dictionaries in
30 your URLconf files and passing those dictionaries as the third member of the
31 URLconf tuple for a given pattern. For example, here's the URLconf for the
32 simple weblog app that drives the blog on djangoproject.com::
34     from django.conf.urls.defaults import *
35     from django_website.apps.blog.models import Entry
37     info_dict = {
38         'queryset': Entry.objects.all(),
39         'date_field': 'pub_date',
40     }
42     urlpatterns = patterns('django.views.generic.date_based',
43        (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', info_dict),
44        (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$',               'archive_day',   info_dict),
45        (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$',                                'archive_month', info_dict),
46        (r'^(?P<year>\d{4})/$',                                                    'archive_year',  info_dict),
47        (r'^$',                                                                    'archive_index', info_dict),
48     )
50 As you can see, this URLconf defines a few options in ``info_dict``.
51 ``'queryset'`` gives the generic view a ``QuerySet`` of objects to use (in this
52 case, all of the ``Entry`` objects) and tells the generic view which model is
53 being used.
55 Documentation of each generic view follows, along with a list of all keyword
56 arguments that a generic view expects. Remember that as in the example above,
57 arguments may either come from the URL pattern (as ``month``, ``day``,
58 ``year``, etc. do above) or from the additional-information dictionary (as for
59 ``queryset``, ``date_field``, etc.).
61 Most generic views require the ``queryset`` key, which is a ``QuerySet``
62 instance; see the `database API docs`_ for more information about ``Queryset``
63 objects.
65 Most views also take an optional ``extra_context`` dictionary that you can use
66 to pass any auxiliary information you wish to the view. The values in the
67 ``extra_context`` dictionary can be either functions (or other callables) or
68 other objects. Functions are evaluated just before they are passed to the
69 template. However, note that QuerySets retrieve and cache their data when they
70 are first evaluated, so if you want to pass in a QuerySet via
71 ``extra_context`` that is always fresh you need to wrap it in a function or
72 lambda that returns the QuerySet.
74 .. _database API docs: ../db-api/
76 "Simple" generic views
77 ======================
79 The ``django.views.generic.simple`` module contains simple views to handle a
80 couple of common cases: rendering a template when no view logic is needed,
81 and issuing a redirect.
83 ``django.views.generic.simple.direct_to_template``
84 --------------------------------------------------
86 **Description:**
88 Renders a given template, passing it a ``{{ params }}`` template variable,
89 which is a dictionary of the parameters captured in the URL.
91 **Required arguments:**
93     * ``template``: The full name of a template to use.
95 **Optional arguments:**
97     * ``extra_context``: A dictionary of values to add to the template
98       context. By default, this is an empty dictionary. If a value in the
99       dictionary is callable, the generic view will call it
100       just before rendering the template.
102     * ``mimetype``: The MIME type to use for the resulting document. Defaults
103       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
105 **Example:**
107 Given the following URL patterns::
109     urlpatterns = patterns('django.views.generic.simple',
110         (r'^foo/$',             'direct_to_template', {'template': 'foo_index.html'}),
111         (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
112     )
114 ... a request to ``/foo/`` would render the template ``foo_index.html``, and a
115 request to ``/foo/15/`` would render the ``foo_detail.html`` with a context
116 variable ``{{ params.id }}`` that is set to ``15``.
118 ``django.views.generic.simple.redirect_to``
119 -------------------------------------------
121 **Description:**
123 Redirects to a given URL.
125 The given URL may contain dictionary-style string formatting, which will be
126 interpolated against the parameters captured in the URL.
128 If the given URL is ``None``, Django will return an ``HttpResponseGone`` (410).
130 **Required arguments:**
132     * ``url``: The URL to redirect to, as a string. Or ``None`` to raise a 410
133       (Gone) HTTP error.
135 **Example:**
137 This example redirects from ``/foo/<id>/`` to ``/bar/<id>/``::
139     urlpatterns = patterns('django.views.generic.simple',
140         ('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}),
141     )
143 This example returns a 410 HTTP error for requests to ``/bar/``::
145     urlpatterns = patterns('django.views.generic.simple',
146         ('^bar/$', 'redirect_to', {'url': None}),
147     )
149 Date-based generic views
150 ========================
152 Date-based generic views (in the module ``django.views.generic.date_based``)
153 are views for displaying drilldown pages for date-based data.
155 ``django.views.generic.date_based.archive_index``
156 -------------------------------------------------
158 **Description:**
160 A top-level index page showing the "latest" objects, by date. Objects with
161 a date in the *future* are not included unless you set ``allow_future`` to
162 ``True``.
164 **Required arguments:**
166     * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
168     * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
169       the ``QuerySet``'s model that the date-based archive should use to
170       determine the objects on the page.
172 **Optional arguments:**
174     * ``num_latest``: The number of latest objects to send to the template
175       context. By default, it's 15.
177     * ``template_name``: The full name of a template to use in rendering the
178       page. This lets you override the default template name (see below).
180     * ``template_loader``: The template loader to use when loading the
181       template. By default, it's ``django.template.loader``.
183     * ``extra_context``: A dictionary of values to add to the template
184       context. By default, this is an empty dictionary. If a value in the
185       dictionary is callable, the generic view will call it
186       just before rendering the template.
188     * ``allow_empty``: A boolean specifying whether to display the page if no
189       objects are available. If this is ``False`` and no objects are available,
190       the view will raise a 404 instead of displaying an empty page. By
191       default, this is ``True``.
193     * ``context_processors``: A list of template-context processors to apply to
194       the view's template. See the `RequestContext docs`_.
196     * ``mimetype``: The MIME type to use for the resulting document. Defaults
197       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
199     * ``allow_future``: A boolean specifying whether to include "future"
200       objects on this page, where "future" means objects in which the field
201       specified in ``date_field`` is greater than the current date/time. By
202       default, this is ``False``.
204     * **New in Django development version:** ``template_object_name``:
205       Designates the name of the template variable to use in the template
206       context. By default, this is ``'latest'``.
208 **Template name:**
210 If ``template_name`` isn't specified, this view will use the template
211 ``<app_label>/<model_name>_archive.html`` by default, where:
213     * ``<model_name>`` is your model's name in all lowercase. For a model
214       ``StaffMember``, that'd be ``staffmember``.
216     * ``<app_label>`` is the right-most part of the full Python path to
217       your model's app. For example, if your model lives in
218       ``apps/blog/models.py``, that'd be ``blog``.
220 **Template context:**
222 In addition to ``extra_context``, the template's context will be:
224     * ``date_list``: A list of ``datetime.date`` objects representing all
225       years that have objects available according to ``queryset``. These are
226       ordered in reverse. This is equivalent to
227       ``queryset.dates(date_field, 'year')[::-1]``.
229     * ``latest``: The ``num_latest`` objects in the system, ordered descending
230       by ``date_field``. For example, if ``num_latest`` is ``10``, then
231       ``latest`` will be a list of the latest 10 objects in ``queryset``.
233       **New in Django development version:** This variable's name depends on
234       the ``template_object_name`` parameter, which is ``'latest'`` by default.
235       If ``template_object_name`` is ``'foo'``, this variable's name will be
236       ``foo``.
238 .. _RequestContext docs: ../templates_python/#subclassing-context-requestcontext
240 ``django.views.generic.date_based.archive_year``
241 ------------------------------------------------
243 **Description:**
245 A yearly archive page showing all available months in a given year. Objects
246 with a date in the *future* are not displayed unless you set ``allow_future``
247 to ``True``.
249 **Required arguments:**
251     * ``year``: The four-digit year for which the archive serves.
253     * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
255     * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
256       the ``QuerySet``'s model that the date-based archive should use to
257       determine the objects on the page.
259 **Optional arguments:**
261     * ``template_name``: The full name of a template to use in rendering the
262       page. This lets you override the default template name (see below).
264     * ``template_loader``: The template loader to use when loading the
265       template. By default, it's ``django.template.loader``.
267     * ``extra_context``: A dictionary of values to add to the template
268       context. By default, this is an empty dictionary. If a value in the
269       dictionary is callable, the generic view will call it
270       just before rendering the template.
272     * ``allow_empty``: A boolean specifying whether to display the page if no
273       objects are available. If this is ``False`` and no objects are available,
274       the view will raise a 404 instead of displaying an empty page. By
275       default, this is ``False``.
277     * ``context_processors``: A list of template-context processors to apply to
278       the view's template. See the `RequestContext docs`_.
280     * ``template_object_name``:  Designates the name of the template variable
281       to use in the template context. By default, this is ``'object'``. The
282       view will append ``'_list'`` to the value of this parameter in
283       determining the variable's name.
285     * ``make_object_list``: A boolean specifying whether to retrieve the full
286       list of objects for this year and pass those to the template. If ``True``,
287       this list of objects will be made available to the template as
288       ``object_list``. (The name ``object_list`` may be different; see the docs
289       for ``object_list`` in the "Template context" section below.) By default,
290       this is ``False``.
292     * ``mimetype``: The MIME type to use for the resulting document. Defaults
293       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
295     * ``allow_future``: A boolean specifying whether to include "future"
296       objects on this page, where "future" means objects in which the field
297       specified in ``date_field`` is greater than the current date/time. By
298       default, this is ``False``.
300 **Template name:**
302 If ``template_name`` isn't specified, this view will use the template
303 ``<app_label>/<model_name>_archive_year.html`` by default.
305 **Template context:**
307 In addition to ``extra_context``, the template's context will be:
309     * ``date_list``: A list of ``datetime.date`` objects representing all
310       months that have objects available in the given year, according to
311       ``queryset``, in ascending order.
313     * ``year``: The given year, as a four-character string.
315     * ``object_list``: If the ``make_object_list`` parameter is ``True``, this
316       will be set to a list of objects available for the given year, ordered by
317       the date field. This variable's name depends on the
318       ``template_object_name`` parameter, which is ``'object'`` by default. If
319       ``template_object_name`` is ``'foo'``, this variable's name will be
320       ``foo_list``.
322       If ``make_object_list`` is ``False``, ``object_list`` will be passed to
323       the template as an empty list.
325 ``django.views.generic.date_based.archive_month``
326 -------------------------------------------------
328 **Description:**
330 A monthly archive page showing all objects in a given month. Objects with a
331 date in the *future* are not displayed unless you set ``allow_future`` to
332 ``True``.
334 **Required arguments:**
336     * ``year``: The four-digit year for which the archive serves (a string).
338     * ``month``: The month for which the archive serves, formatted according to
339       the ``month_format`` argument.
341     * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
343     * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
344       the ``QuerySet``'s model that the date-based archive should use to
345       determine the objects on the page.
347 **Optional arguments:**
349     * ``month_format``: A format string that regulates what format the
350       ``month`` parameter uses. This should be in the syntax accepted by
351       Python's ``time.strftime``. (See the `strftime docs`_.) It's set to
352       ``"%b"`` by default, which is a three-letter month abbreviation. To
353       change it to use numbers, use ``"%m"``.
355     * ``template_name``: The full name of a template to use in rendering the
356       page. This lets you override the default template name (see below).
358     * ``template_loader``: The template loader to use when loading the
359       template. By default, it's ``django.template.loader``.
361     * ``extra_context``: A dictionary of values to add to the template
362       context. By default, this is an empty dictionary. If a value in the
363       dictionary is callable, the generic view will call it
364       just before rendering the template.
366     * ``allow_empty``: A boolean specifying whether to display the page if no
367       objects are available. If this is ``False`` and no objects are available,
368       the view will raise a 404 instead of displaying an empty page. By
369       default, this is ``False``.
371     * ``context_processors``: A list of template-context processors to apply to
372       the view's template. See the `RequestContext docs`_.
374     * ``template_object_name``:  Designates the name of the template variable
375       to use in the template context. By default, this is ``'object'``. The
376       view will append ``'_list'`` to the value of this parameter in
377       determining the variable's name.
379     * ``mimetype``: The MIME type to use for the resulting document. Defaults
380       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
382     * ``allow_future``: A boolean specifying whether to include "future"
383       objects on this page, where "future" means objects in which the field
384       specified in ``date_field`` is greater than the current date/time. By
385       default, this is ``False``.
387 **Template name:**
389 If ``template_name`` isn't specified, this view will use the template
390 ``<app_label>/<model_name>_archive_month.html`` by default.
392 **Template context:**
394 In addition to ``extra_context``, the template's context will be:
396     * ``month``: A ``datetime.date`` object representing the given month.
398     * ``next_month``: A ``datetime.date`` object representing the first day of
399       the next month. If the next month is in the future, this will be
400       ``None``.
402     * ``previous_month``: A ``datetime.date`` object representing the first day
403       of the previous month. Unlike ``next_month``, this will never be
404       ``None``.
406     * ``object_list``: A list of objects available for the given month. This
407       variable's name depends on the ``template_object_name`` parameter, which
408       is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
409       this variable's name will be ``foo_list``.
411 .. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
413 ``django.views.generic.date_based.archive_week``
414 ------------------------------------------------
416 **Description:**
418 A weekly archive page showing all objects in a given week. Objects with a date
419 in the *future* are not displayed unless you set ``allow_future`` to ``True``.
421 **Required arguments:**
423     * ``year``: The four-digit year for which the archive serves (a string).
425     * ``week``: The week of the year for which the archive serves (a string).
426       Weeks start with Sunday.
428     * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
430     * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
431       the ``QuerySet``'s model that the date-based archive should use to
432       determine the objects on the page.
434 **Optional arguments:**
436     * ``template_name``: The full name of a template to use in rendering the
437       page. This lets you override the default template name (see below).
439     * ``template_loader``: The template loader to use when loading the
440       template. By default, it's ``django.template.loader``.
442     * ``extra_context``: A dictionary of values to add to the template
443       context. By default, this is an empty dictionary. If a value in the
444       dictionary is callable, the generic view will call it
445       just before rendering the template.
447     * ``allow_empty``: A boolean specifying whether to display the page if no
448       objects are available. If this is ``False`` and no objects are available,
449       the view will raise a 404 instead of displaying an empty page. By
450       default, this is ``True``.
452     * ``context_processors``: A list of template-context processors to apply to
453       the view's template. See the `RequestContext docs`_.
455     * ``template_object_name``:  Designates the name of the template variable
456       to use in the template context. By default, this is ``'object'``. The
457       view will append ``'_list'`` to the value of this parameter in
458       determining the variable's name.
460     * ``mimetype``: The MIME type to use for the resulting document. Defaults
461       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
463     * ``allow_future``: A boolean specifying whether to include "future"
464       objects on this page, where "future" means objects in which the field
465       specified in ``date_field`` is greater than the current date/time. By
466       default, this is ``False``.
468 **Template name:**
470 If ``template_name`` isn't specified, this view will use the template
471 ``<app_label>/<model_name>_archive_week.html`` by default.
473 **Template context:**
475 In addition to ``extra_context``, the template's context will be:
477     * ``week``: A ``datetime.date`` object representing the first day of the
478       given week.
480     * ``object_list``: A list of objects available for the given week. This
481       variable's name depends on the ``template_object_name`` parameter, which
482       is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
483       this variable's name will be ``foo_list``.
485 ``django.views.generic.date_based.archive_day``
486 -----------------------------------------------
488 **Description:**
490 A day archive page showing all objects in a given day. Days in the future throw
491 a 404 error, regardless of whether any objects exist for future days, unless
492 you set ``allow_future`` to ``True``.
494 **Required arguments:**
496     * ``year``: The four-digit year for which the archive serves (a string).
498     * ``month``: The month for which the archive serves, formatted according to
499       the ``month_format`` argument.
501     * ``day``: The day for which the archive serves, formatted according to the
502       ``day_format`` argument.
504     * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
506     * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
507       the ``QuerySet``'s model that the date-based archive should use to
508       determine the objects on the page.
510 **Optional arguments:**
512     * ``month_format``: A format string that regulates what format the
513       ``month`` parameter uses. This should be in the syntax accepted by
514       Python's ``time.strftime``. (See the `strftime docs`_.) It's set to
515       ``"%b"`` by default, which is a three-letter month abbreviation. To
516       change it to use numbers, use ``"%m"``.
518     * ``day_format``: Like ``month_format``, but for the ``day`` parameter.
519       It defaults to ``"%d"`` (day of the month as a decimal number, 01-31).
521     * ``template_name``: The full name of a template to use in rendering the
522       page. This lets you override the default template name (see below).
524     * ``template_loader``: The template loader to use when loading the
525       template. By default, it's ``django.template.loader``.
527     * ``extra_context``: A dictionary of values to add to the template
528       context. By default, this is an empty dictionary. If a value in the
529       dictionary is callable, the generic view will call it
530       just before rendering the template.
532     * ``allow_empty``: A boolean specifying whether to display the page if no
533       objects are available. If this is ``False`` and no objects are available,
534       the view will raise a 404 instead of displaying an empty page. By
535       default, this is ``False``.
537     * ``context_processors``: A list of template-context processors to apply to
538       the view's template. See the `RequestContext docs`_.
540     * ``template_object_name``:  Designates the name of the template variable
541       to use in the template context. By default, this is ``'object'``. The
542       view will append ``'_list'`` to the value of this parameter in
543       determining the variable's name.
545     * ``mimetype``: The MIME type to use for the resulting document. Defaults
546       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
548     * ``allow_future``: A boolean specifying whether to include "future"
549       objects on this page, where "future" means objects in which the field
550       specified in ``date_field`` is greater than the current date/time. By
551       default, this is ``False``.
553 **Template name:**
555 If ``template_name`` isn't specified, this view will use the template
556 ``<app_label>/<model_name>_archive_day.html`` by default.
558 **Template context:**
560 In addition to ``extra_context``, the template's context will be:
562     * ``day``: A ``datetime.date`` object representing the given day.
564     * ``next_day``: A ``datetime.date`` object representing the next day. If
565       the next day is in the future, this will be ``None``.
567     * ``previous_day``: A ``datetime.date`` object representing the given day.
568       Unlike ``next_day``, this will never be ``None``.
570     * ``object_list``: A list of objects available for the given day. This
571       variable's name depends on the ``template_object_name`` parameter, which
572       is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
573       this variable's name will be ``foo_list``.
575 ``django.views.generic.date_based.archive_today``
576 -------------------------------------------------
578 **Description:**
580 A day archive page showing all objects for *today*. This is exactly the same as
581 ``archive_day``, except the ``year``/``month``/``day`` arguments are not used,
582 and today's date is used instead.
584 ``django.views.generic.date_based.object_detail``
585 -------------------------------------------------
587 **Description:**
589 A page representing an individual object. If the object has a date value in the
590 future, the view will throw a 404 error by default, unless you set
591 ``allow_future`` to ``True``.
593 **Required arguments:**
595     * ``year``: The object's four-digit year (a string).
597     * ``month``: The object's month , formatted according to the
598       ``month_format`` argument.
600     * ``day``: The object's day , formatted according to the ``day_format``
601       argument.
603     * ``queryset``: A ``QuerySet`` that contains the object.
605     * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
606       the ``QuerySet``'s model that the generic view should use to look up the
607       object according to ``year``, ``month`` and ``day``.
609     * Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
611       If you provide ``object_id``, it should be the value of the primary-key
612       field for the object being displayed on this page.
614       Otherwise, ``slug`` should be the slug of the given object, and
615       ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
616       model. By default, ``slug_field`` is ``'slug'``.
618 **Optional arguments:**
620     * ``month_format``: A format string that regulates what format the
621       ``month`` parameter uses. This should be in the syntax accepted by
622       Python's ``time.strftime``. (See the `strftime docs`_.) It's set to
623       ``"%b"`` by default, which is a three-letter month abbreviation. To
624       change it to use numbers, use ``"%m"``.
626     * ``day_format``: Like ``month_format``, but for the ``day`` parameter.
627       It defaults to ``"%d"`` (day of the month as a decimal number, 01-31).
629     * ``template_name``: The full name of a template to use in rendering the
630       page. This lets you override the default template name (see below).
632     * ``template_name_field``: The name of a field on the object whose value is
633       the template name to use. This lets you store template names in the data.
634       In other words, if your object has a field ``'the_template'`` that
635       contains a string ``'foo.html'``, and you set ``template_name_field`` to
636       ``'the_template'``, then the generic view for this object will use the
637       template ``'foo.html'``.
639       It's a bit of a brain-bender, but it's useful in some cases.
641     * ``template_loader``: The template loader to use when loading the
642       template. By default, it's ``django.template.loader``.
644     * ``extra_context``: A dictionary of values to add to the template
645       context. By default, this is an empty dictionary. If a value in the
646       dictionary is callable, the generic view will call it
647       just before rendering the template.
649     * ``context_processors``: A list of template-context processors to apply to
650       the view's template. See the `RequestContext docs`_.
652     * ``template_object_name``:  Designates the name of the template variable
653       to use in the template context. By default, this is ``'object'``.
655     * ``mimetype``: The MIME type to use for the resulting document. Defaults
656       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
658     * ``allow_future``: A boolean specifying whether to include "future"
659       objects on this page, where "future" means objects in which the field
660       specified in ``date_field`` is greater than the current date/time. By
661       default, this is ``False``.
663 **Template name:**
665 If ``template_name`` isn't specified, this view will use the template
666 ``<app_label>/<model_name>_detail.html`` by default.
668 **Template context:**
670 In addition to ``extra_context``, the template's context will be:
672     * ``object``: The object. This variable's name depends on the
673       ``template_object_name`` parameter, which is ``'object'`` by default. If
674       ``template_object_name`` is ``'foo'``, this variable's name will be
675       ``foo``.
677 List/detail generic views
678 =========================
680 The list-detail generic-view framework (in the
681 ``django.views.generic.list_detail`` module) is similar to the date-based one,
682 except the former simply has two views: a list of objects and an individual
683 object page.
685 ``django.views.generic.list_detail.object_list``
686 ------------------------------------------------
688 **Description:**
690 A page representing a list of objects.
692 **Required arguments:**
694     * ``queryset``: A ``QuerySet`` that represents the objects.
696 **Optional arguments:**
698     * ``paginate_by``: An integer specifying how many objects should be
699       displayed per page. If this is given, the view will paginate objects with
700       ``paginate_by`` objects per page. The view will expect either a ``page``
701       query string parameter (via ``GET``) or a ``page`` variable specified in
702       the URLconf. See `Notes on pagination`_ below.
704     * ``page``: The current page number, as an integer. This is 1-based. 
705       See `Notes on pagination`_ below.
707     * ``template_name``: The full name of a template to use in rendering the
708       page. This lets you override the default template name (see below).
710     * ``template_loader``: The template loader to use when loading the
711       template. By default, it's ``django.template.loader``.
713     * ``extra_context``: A dictionary of values to add to the template
714       context. By default, this is an empty dictionary. If a value in the
715       dictionary is callable, the generic view will call it
716       just before rendering the template.
718     * ``allow_empty``: A boolean specifying whether to display the page if no
719       objects are available. If this is ``False`` and no objects are available,
720       the view will raise a 404 instead of displaying an empty page. By
721       default, this is ``True``.
723     * ``context_processors``: A list of template-context processors to apply to
724       the view's template. See the `RequestContext docs`_.
726     * ``template_object_name``:  Designates the name of the template variable
727       to use in the template context. By default, this is ``'object'``. The
728       view will append ``'_list'`` to the value of this parameter in
729       determining the variable's name.
731     * ``mimetype``: The MIME type to use for the resulting document. Defaults
732       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
734 **Template name:**
736 If ``template_name`` isn't specified, this view will use the template
737 ``<app_label>/<model_name>_list.html`` by default.
739 **Template context:**
741 In addition to ``extra_context``, the template's context will be:
743     * ``object_list``: The list of objects. This variable's name depends on the
744       ``template_object_name`` parameter, which is ``'object'`` by default. If
745       ``template_object_name`` is ``'foo'``, this variable's name will be
746       ``foo_list``.
748     * ``is_paginated``: A boolean representing whether the results are
749       paginated. Specifically, this is set to ``False`` if the number of
750       available objects is less than or equal to ``paginate_by``.
752 If the results are paginated, the context will contain these extra variables:
754     * **New in Django development version:** ``paginator``: An instance of
755       ``django.core.paginator.Paginator``.
757     * **New in Django development version:** ``page_obj``: An instance of
758       ``django.core.paginator.Page``.
760 In older versions of Django, before ``paginator`` and ``page_obj`` were added
761 to this template's context, the template included several other variables
762 related to pagination. Note that you should *NOT* use these variables anymore;
763 use ``paginator`` and ``page_obj`` instead, because they let you do everything
764 these old variables let you do (and more!). But for legacy installations,
765 here's a list of those old template variables:
767     * ``results_per_page``: The number of objects per page. (Same as the
768       ``paginate_by`` parameter.)
770     * ``has_next``: A boolean representing whether there's a next page.
772     * ``has_previous``: A boolean representing whether there's a previous page.
774     * ``page``: The current page number, as an integer. This is 1-based.
776     * ``next``: The next page number, as an integer. If there's no next page,
777       this will still be an integer representing the theoretical next-page
778       number. This is 1-based.
780     * ``previous``: The previous page number, as an integer. This is 1-based.
782     * ``last_on_page``: The number of the
783       last result on the current page. This is 1-based.
785     * ``first_on_page``: The number of the
786       first result on the current page. This is 1-based.
788     * ``pages``: The total number of pages, as an integer.
790     * ``hits``: The total number of objects across *all* pages, not just this
791       page.
793     * ``page_range``: A list of the page numbers that are available. This is
794       1-based.
796 Notes on pagination
797 ~~~~~~~~~~~~~~~~~~~
799 If ``paginate_by`` is specified, Django will paginate the results. You can
800 specify the page number in the URL in one of two ways:
802     * Use the ``page`` parameter in the URLconf. For example, this is what
803       your URLconf might look like::
805         (r'^objects/page(?P<page>[0-9]+)/$', 'object_list', dict(info_dict))
807     * Pass the page number via the ``page`` query-string parameter. For
808       example, a URL would look like this::
810         /objects/?page=3
812     * To loop over all the available page numbers, use the ``page_range`` 
813       variable. You can iterate over the list provided by ``page_range`` 
814       to create a link to every page of results.
816 These values and lists are 1-based, not 0-based, so the first page would be
817 represented as page ``1``. 
819 An example of the use of pagination can be found in the `object pagination`_
820 example model. 
821          
822 .. _`object pagination`: ../models/pagination/
824 **New in Django development version:** 
826 As a special case, you are also permitted to use 
827 ``last`` as a value for ``page``::
829     /objects/?page=last
831 This allows you to access the final page of results without first having to 
832 determine how many pages there are.
834 Note that ``page`` *must* be either a valid page number or the value ``last``;
835 any other value for ``page`` will result in a 404 error.
837 ``django.views.generic.list_detail.object_detail``
838 --------------------------------------------------
840 A page representing an individual object.
842 **Description:**
844 A page representing an individual object.
846 **Required arguments:**
848     * ``queryset``: A ``QuerySet`` that contains the object.
850     * Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
852       If you provide ``object_id``, it should be the value of the primary-key
853       field for the object being displayed on this page.
855       Otherwise, ``slug`` should be the slug of the given object, and
856       ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
857       model. By default, ``slug_field`` is ``'slug'``.
859 **Optional arguments:**
861     * ``template_name``: The full name of a template to use in rendering the
862       page. This lets you override the default template name (see below).
864     * ``template_name_field``: The name of a field on the object whose value is
865       the template name to use. This lets you store template names in the data.
866       In other words, if your object has a field ``'the_template'`` that
867       contains a string ``'foo.html'``, and you set ``template_name_field`` to
868       ``'the_template'``, then the generic view for this object will use the
869       template ``'foo.html'``.
871       It's a bit of a brain-bender, but it's useful in some cases.
873     * ``template_loader``: The template loader to use when loading the
874       template. By default, it's ``django.template.loader``.
876     * ``extra_context``: A dictionary of values to add to the template
877       context. By default, this is an empty dictionary. If a value in the
878       dictionary is callable, the generic view will call it
879       just before rendering the template.
881     * ``context_processors``: A list of template-context processors to apply to
882       the view's template. See the `RequestContext docs`_.
884     * ``template_object_name``:  Designates the name of the template variable
885       to use in the template context. By default, this is ``'object'``.
887     * ``mimetype``: The MIME type to use for the resulting document. Defaults
888       to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
890 **Template name:**
892 If ``template_name`` isn't specified, this view will use the template
893 ``<app_label>/<model_name>_detail.html`` by default.
895 **Template context:**
897 In addition to ``extra_context``, the template's context will be:
899     * ``object``: The object. This variable's name depends on the
900       ``template_object_name`` parameter, which is ``'object'`` by default. If
901       ``template_object_name`` is ``'foo'``, this variable's name will be
902       ``foo``.
904 Create/update/delete generic views
905 ==================================
907 The ``django.views.generic.create_update`` module contains a set of functions
908 for creating, editing and deleting objects.
910 ``django.views.generic.create_update.create_object``
911 ----------------------------------------------------
913 **Description:**
915 A page that displays a form for creating an object, redisplaying the form with
916 validation errors (if there are any) and saving the object. This uses the
917 automatic manipulators that come with Django models.
919 **Required arguments:**
921     * ``model``: The Django model class of the object that the form will
922       create.
924 **Optional arguments:**
926     * ``post_save_redirect``: A URL to which the view will redirect after
927       saving the object. By default, it's ``object.get_absolute_url()``.
929       ``post_save_redirect`` may contain dictionary string formatting, which
930       will be interpolated against the object's field attributes. For example,
931       you could use ``post_save_redirect="/polls/%(slug)s/"``.
933     * ``login_required``: A boolean that designates whether a user must be
934       logged in, in order to see the page and save changes. This hooks into the
935       Django `authentication system`_. By default, this is ``False``.
937       If this is ``True``, and a non-logged-in user attempts to visit this page
938       or save the form, Django will redirect the request to ``/accounts/login/``.
940     * ``template_name``: The full name of a template to use in rendering the
941       page. This lets you override the default template name (see below).
943     * ``template_loader``: The template loader to use when loading the
944       template. By default, it's ``django.template.loader``.
946     * ``extra_context``: A dictionary of values to add to the template
947       context. By default, this is an empty dictionary. If a value in the
948       dictionary is callable, the generic view will call it
949       just before rendering the template.
951     * ``context_processors``: A list of template-context processors to apply to
952       the view's template. See the `RequestContext docs`_.
954 **Template name:**
956 If ``template_name`` isn't specified, this view will use the template
957 ``<app_label>/<model_name>_form.html`` by default.
959 **Template context:**
961 In addition to ``extra_context``, the template's context will be:
963     * ``form``: A ``django.oldforms.FormWrapper`` instance representing the form
964       for editing the object. This lets you refer to form fields easily in the
965       template system.
967       For example, if ``model`` has two fields, ``name`` and ``address``::
969           <form action="" method="post">
970           <p><label for="id_name">Name:</label> {{ form.name }}</p>
971           <p><label for="id_address">Address:</label> {{ form.address }}</p>
972           </form>
974       See the `manipulator and formfield documentation`_ for more information
975       about using ``FormWrapper`` objects in templates.
977 .. _authentication system: ../authentication/
978 .. _manipulator and formfield documentation: ../forms/
980 ``django.views.generic.create_update.update_object``
981 ----------------------------------------------------
983 **Description:**
985 A page that displays a form for editing an existing object, redisplaying the
986 form with validation errors (if there are any) and saving changes to the
987 object. This uses the automatic manipulators that come with Django models.
989 **Required arguments:**
991     * ``model``: The Django model class of the object that the form will
992       create.
994     * Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
996       If you provide ``object_id``, it should be the value of the primary-key
997       field for the object being displayed on this page.
999       Otherwise, ``slug`` should be the slug of the given object, and
1000       ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
1001       model. By default, ``slug_field`` is ``'slug'``.
1003 **Optional arguments:**
1005     * ``post_save_redirect``: A URL to which the view will redirect after
1006       saving the object. By default, it's ``object.get_absolute_url()``.
1008       ``post_save_redirect`` may contain dictionary string formatting, which
1009       will be interpolated against the object's field attributes. For example,
1010       you could use ``post_save_redirect="/polls/%(slug)s/"``.
1012     * ``login_required``: A boolean that designates whether a user must be
1013       logged in, in order to see the page and save changes. This hooks into the
1014       Django `authentication system`_. By default, this is ``False``.
1016       If this is ``True``, and a non-logged-in user attempts to visit this page
1017       or save the form, Django will redirect the request to ``/accounts/login/``.
1019     * ``template_name``: The full name of a template to use in rendering the
1020       page. This lets you override the default template name (see below).
1022     * ``template_loader``: The template loader to use when loading the
1023       template. By default, it's ``django.template.loader``.
1025     * ``extra_context``: A dictionary of values to add to the template
1026       context. By default, this is an empty dictionary. If a value in the
1027       dictionary is callable, the generic view will call it
1028       just before rendering the template.
1030     * ``context_processors``: A list of template-context processors to apply to
1031       the view's template. See the `RequestContext docs`_.
1033     * ``template_object_name``:  Designates the name of the template variable
1034       to use in the template context. By default, this is ``'object'``.
1036 **Template name:**
1038 If ``template_name`` isn't specified, this view will use the template
1039 ``<app_label>/<model_name>_form.html`` by default.
1041 **Template context:**
1043 In addition to ``extra_context``, the template's context will be:
1045     * ``form``: A ``django.oldforms.FormWrapper`` instance representing the form
1046       for editing the object. This lets you refer to form fields easily in the
1047       template system.
1049       For example, if ``model`` has two fields, ``name`` and ``address``::
1051           <form action="" method="post">
1052           <p><label for="id_name">Name:</label> {{ form.name }}</p>
1053           <p><label for="id_address">Address:</label> {{ form.address }}</p>
1054           </form>
1056       See the `manipulator and formfield documentation`_ for more information
1057       about using ``FormWrapper`` objects in templates.
1059     * ``object``: The original object being edited. This variable's name
1060       depends on the ``template_object_name`` parameter, which is ``'object'``
1061       by default. If ``template_object_name`` is ``'foo'``, this variable's
1062       name will be ``foo``.
1064 ``django.views.generic.create_update.delete_object``
1065 ----------------------------------------------------
1067 **Description:**
1069 A view that displays a confirmation page and deletes an existing object. The
1070 given object will only be deleted if the request method is ``POST``. If this
1071 view is fetched via ``GET``, it will display a confirmation page that should
1072 contain a form that POSTs to the same URL.
1074 **Required arguments:**
1076     * ``model``: The Django model class of the object that the form will
1077       create.
1079     * Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
1081       If you provide ``object_id``, it should be the value of the primary-key
1082       field for the object being displayed on this page.
1084       Otherwise, ``slug`` should be the slug of the given object, and
1085       ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
1086       model. By default, ``slug_field`` is ``'slug'``.
1088     * ``post_delete_redirect``: A URL to which the view will redirect after
1089       deleting the object.
1091 **Optional arguments:**
1093     * ``login_required``: A boolean that designates whether a user must be
1094       logged in, in order to see the page and save changes. This hooks into the
1095       Django `authentication system`_. By default, this is ``False``.
1097       If this is ``True``, and a non-logged-in user attempts to visit this page
1098       or save the form, Django will redirect the request to ``/accounts/login/``.
1100     * ``template_name``: The full name of a template to use in rendering the
1101       page. This lets you override the default template name (see below).
1103     * ``template_loader``: The template loader to use when loading the
1104       template. By default, it's ``django.template.loader``.
1106     * ``extra_context``: A dictionary of values to add to the template
1107       context. By default, this is an empty dictionary. If a value in the
1108       dictionary is callable, the generic view will call it
1109       just before rendering the template.
1111     * ``context_processors``: A list of template-context processors to apply to
1112       the view's template. See the `RequestContext docs`_.
1114     * ``template_object_name``:  Designates the name of the template variable
1115       to use in the template context. By default, this is ``'object'``.
1117 **Template name:**
1119 If ``template_name`` isn't specified, this view will use the template
1120 ``<app_label>/<model_name>_confirm_delete.html`` by default.
1122 **Template context:**
1124 In addition to ``extra_context``, the template's context will be:
1126     * ``object``: The original object that's about to be deleted. This
1127       variable's name depends on the ``template_object_name`` parameter, which
1128       is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
1129       this variable's name will be ``foo``.