Django

Code

root/django/branches/gis/docs/generic_views.txt

Revision 8215, 44.2 kB (checked in by jbronn, 4 months ago)

gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.

  • Property svn:eol-style set to native
Line 
1 =============
2 Generic views
3 =============
4
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.
9
10 Django's generic views contain the following:
11
12     * A set of views for doing list/detail interfaces (for example,
13       Django's `documentation index`_ and `detail pages`_).
14
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).
18
19     * A set of views for creating, editing, and deleting objects.
20
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/
28
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::
33
34     from django.conf.urls.defaults import *
35     from django_website.apps.blog.models import Entry
36
37     info_dict = {
38         'queryset': Entry.objects.all(),
39         'date_field': 'pub_date',
40     }
41
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     )
49
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.
54
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.).
60
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.
64
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.
73
74 .. _database API docs: ../db-api/
75
76 "Simple" generic views
77 ======================
78
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.
82
83 ``django.views.generic.simple.direct_to_template``
84 --------------------------------------------------
85
86 Description
87 ~~~~~~~~~~~
88
89 Renders a given template, passing it a ``{{ params }}`` template variable,
90 which is a dictionary of the parameters captured in the URL.
91
92 Required arguments
93 ~~~~~~~~~~~~~~~~~~
94
95 ``template``
96     The full name of a template to use.
97
98 Optional arguments
99 ~~~~~~~~~~~~~~~~~~
100
101 ``extra_context``
102     A dictionary of values to add to the template context. By default, this is
103     an empty dictionary. If a value in the dictionary is callable, the generic
104     view will call it just before rendering the template.
105
106 ``mimetype``
107     The MIME type to use for the resulting document. Defaults to the value of
108     the ``DEFAULT_CONTENT_TYPE`` setting.
109
110 **Example:**
111
112 Given the following URL patterns::
113
114     urlpatterns = patterns('django.views.generic.simple',
115         (r'^foo/$',             'direct_to_template', {'template': 'foo_index.html'}),
116         (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
117     )
118
119 ... a request to ``/foo/`` would render the template ``foo_index.html``, and a
120 request to ``/foo/15/`` would render the ``foo_detail.html`` with a context
121 variable ``{{ params.id }}`` that is set to ``15``.
122
123 ``django.views.generic.simple.redirect_to``
124 -------------------------------------------
125
126 Description
127 ~~~~~~~~~~~
128
129 Redirects to a given URL.
130
131 The given URL may contain dictionary-style string formatting, which will be
132 interpolated against the parameters captured in the URL.
133
134 If the given URL is ``None``, Django will return an ``HttpResponseGone`` (410).
135
136 Required arguments
137 ~~~~~~~~~~~~~~~~~~
138
139 ``url``
140     The URL to redirect to, as a string. Or ``None`` to raise a 410 (Gone)
141     HTTP error.
142
143 **Example:**
144
145 This example redirects from ``/foo/<id>/`` to ``/bar/<id>/``::
146
147     urlpatterns = patterns('django.views.generic.simple',
148         ('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}),
149     )
150
151 This example returns a 410 HTTP error for requests to ``/bar/``::
152
153     urlpatterns = patterns('django.views.generic.simple',
154         ('^bar/$', 'redirect_to', {'url': None}),
155     )
156
157 Date-based generic views
158 ========================
159
160 Date-based generic views (in the module ``django.views.generic.date_based``)
161 are views for displaying drilldown pages for date-based data.
162
163 ``django.views.generic.date_based.archive_index``
164 -------------------------------------------------
165
166 Description
167 ~~~~~~~~~~~
168
169 A top-level index page showing the "latest" objects, by date. Objects with
170 a date in the *future* are not included unless you set ``allow_future`` to
171 ``True``.
172
173 Required arguments
174 ~~~~~~~~~~~~~~~~~~
175
176 ``queryset``
177     A ``QuerySet`` of objects for which the archive serves.
178
179 ``date_field``
180     The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
181     model that the date-based archive should use to determine the objects on
182     the page.
183
184 Optional arguments
185 ~~~~~~~~~~~~~~~~~~
186
187 ``num_latest``
188     The number of latest objects to send to the template context. By default,
189     it's 15.
190
191 ``template_name``
192     The full name of a template to use in rendering the page. This lets you
193     override the default template name (see below).
194
195 ``template_loader``
196     The template loader to use when loading the template. By default, it's
197     ``django.template.loader``.
198
199 ``extra_context``
200     A dictionary of values to add to the template context. By default, this is
201     an empty dictionary. If a value in the dictionary is callable, the generic
202     view will call it just before rendering the template.
203
204 ``allow_empty``
205     A boolean specifying whether to display the page if no objects are
206     available. If this is ``False`` and no objects are available, the view will
207     raise a 404 instead of displaying an empty page. By default, this is
208     ``True``.
209
210 ``context_processors``
211     A list of template-context processors to apply to the view's template. See
212     the `RequestContext docs`_.
213
214 ``mimetype``
215     The MIME type to use for the resulting document. Defaults to the value of
216     the ``DEFAULT_CONTENT_TYPE`` setting.
217
218 ``allow_future``
219     A boolean specifying whether to include "future" objects on this page,
220     where "future" means objects in which the field specified in ``date_field``
221     is greater than the current date/time. By default, this is ``False``.
222
223 ``template_object_name`` (**New in Django development version**)
224     Designates the name of the template variable to use in the template
225     context. By default, this is ``'latest'``.
226
227 Template name
228 ~~~~~~~~~~~~~
229
230 If ``template_name`` isn't specified, this view will use the template
231 ``<app_label>/<model_name>_archive.html`` by default, where:
232
233     * ``<model_name>`` is your model's name in all lowercase. For a model
234       ``StaffMember``, that'd be ``staffmember``.
235
236     * ``<app_label>`` is the right-most part of the full Python path to
237       your model's app. For example, if your model lives in
238       ``apps/blog/models.py``, that'd be ``blog``.
239
240 Template context
241 ~~~~~~~~~~~~~~~~
242
243 In addition to ``extra_context``, the template's context will be:
244
245 ``date_list``
246     A list of ``datetime.date`` objects representing all years that have
247     objects available according to ``queryset``. These are ordered in reverse.
248     This is equivalent to ``queryset.dates(date_field, 'year')[::-1]``.
249
250 ``latest``
251     The ``num_latest`` objects in the system, ordered descending by
252     ``date_field``. For example, if ``num_latest`` is ``10``, then ``latest``
253     will be a list of the latest 10 objects in ``queryset``.
254
255     **New in Django development version:** This variable's name depends on the
256     ``template_object_name`` parameter, which is ``'latest'`` by default.
257     If ``template_object_name`` is ``'foo'``, this variable's name will be
258     ``foo``.
259
260 .. _RequestContext docs: ../templates_python/#subclassing-context-requestcontext
261
262 ``django.views.generic.date_based.archive_year``
263 ------------------------------------------------
264
265 Description
266 ~~~~~~~~~~~
267
268 A yearly archive page showing all available months in a given year. Objects
269 with a date in the *future* are not displayed unless you set ``allow_future``
270 to ``True``.
271
272 Required arguments
273 ~~~~~~~~~~~~~~~~~~
274
275 ``year``
276     The four-digit year for which the archive serves.
277
278 ``queryset``
279     A ``QuerySet`` of objects for which the archive serves.
280
281 ``date_field``
282     The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
283     model that the date-based archive should use to determine the objects on
284     the page.
285
286 Optional arguments
287 ~~~~~~~~~~~~~~~~~~
288
289 ``template_name``
290     The full name of a template to use in rendering the page. This lets you
291     override the default template name (see below).
292
293 ``template_loader``
294     The template loader to use when loading the template. By default, it's
295     ``django.template.loader``.
296
297 ``extra_context``
298     A dictionary of values to add to the template context. By default, this is
299     an empty dictionary. If a value in the dictionary is callable, the generic
300     view will call it just before rendering the template.
301
302 ``allow_empty``
303     A boolean specifying whether to display the page if no objects are
304     available. If this is ``False`` and no objects are available, the view will
305     raise a 404 instead of displaying an empty page. By default, this is
306     ``False``.
307
308 ``context_processors``
309     A list of template-context processors to apply to the view's template. See
310     the `RequestContext docs`_.
311
312 ``template_object_name``
313     Designates the name of the template variable to use in the template
314     context. By default, this is ``'object'``. The view will append ``'_list'``
315     to the value of this parameter in determining the variable's name.
316
317 ``make_object_list``
318     A boolean specifying whether to retrieve the full  list of objects for this
319     year and pass those to the template. If ``True``, this list of objects will
320     be made available to the template as ``object_list``. (The name
321     ``object_list`` may be different; see the docs for ``object_list`` in the
322     "Template context" section below.) By default, this is ``False``.
323
324 ``mimetype``
325     The MIME type to use for the resulting document. Defaults to the value of
326     the ``DEFAULT_CONTENT_TYPE`` setting.
327
328 ``allow_future``
329     A boolean specifying whether to include "future" objects on this page,
330     where "future" means objects in which the field specified in ``date_field``
331     is greater than the current date/time. By default, this is ``False``.
332
333 Template name
334 ~~~~~~~~~~~~~
335
336 If ``template_name`` isn't specified, this view will use the template
337 ``<app_label>/<model_name>_archive_year.html`` by default.
338
339 Template context
340 ~~~~~~~~~~~~~~~~
341
342 In addition to ``extra_context``, the template's context will be:
343
344 ``date_list``
345     A list of ``datetime.date`` objects representing all months that have
346     objects available in the given year, according to ``queryset``, in
347     ascending order.
348
349 ``year``
350     The given year, as a four-character string.
351
352 ``object_list``
353     If the ``make_object_list`` parameter is ``True``, this will be set to a
354     list of objects available for the given year, ordered by the date field.
355     This variable's name depends on the ``template_object_name`` parameter,
356     which is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
357     this variable's name will be ``foo_list``.
358
359     If ``make_object_list`` is ``False``, ``object_list`` will be passed to the
360     template as an empty list.
361
362 ``django.views.generic.date_based.archive_month``
363 -------------------------------------------------
364
365 Description
366 ~~~~~~~~~~~
367
368 A monthly archive page showing all objects in a given month. Objects with a
369 date in the *future* are not displayed unless you set ``allow_future`` to
370 ``True``.
371
372 Required arguments
373 ~~~~~~~~~~~~~~~~~~
374
375 ``year``
376     The four-digit year for which the archive serves (a string).
377
378 ``month``
379     The month for which the archive serves, formatted according to the
380     ``month_format`` argument.
381
382 ``queryset``
383     A ``QuerySet`` of objects for which the archive serves.
384
385 ``date_field``
386     The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
387     model that the date-based archive should use to determine the objects on
388     the page.
389
390 Optional arguments
391 ~~~~~~~~~~~~~~~~~~
392
393 ``month_format``
394     A format string that regulates what format the ``month`` parameter uses.
395     This should be in the syntax accepted by Python's ``time.strftime``. (See
396     the `strftime docs`_.) It's set to ``"%b"`` by default, which is a three-
397     letter month abbreviation. To change it to use numbers, use ``"%m"``.
398
399 ``template_name``
400     The full name of a template to use in rendering the page. This lets you
401     override the default template name (see below).
402
403 ``template_loader``
404     The template loader to use when loading the template. By default, it's
405     ``django.template.loader``.
406
407 ``extra_context``
408     A dictionary of values to add to the template context. By default, this is
409     an empty dictionary. If a value in the dictionary is callable, the generic
410     view will call it just before rendering the template.
411
412 ``allow_empty``
413     A boolean specifying whether to display the page if no objects are
414     available. If this is ``False`` and no objects are available, the view will
415     raise a 404 instead of displaying an empty page. By default, this is
416     ``False``.
417
418 ``context_processors``
419     A list of template-context processors to apply to the view's template. See
420     the `RequestContext docs`_.
421
422 ``template_object_name``
423     Designates the name of the template variable to use in the template
424     context. By default, this is ``'object'``. The view will append ``'_list'``
425     to the value of this parameter in determining the variable's name.
426
427 ``mimetype``
428     The MIME type to use for the resulting document. Defaults to the value of
429     the ``DEFAULT_CONTENT_TYPE`` setting.
430
431 ``allow_future``
432     A boolean specifying whether to include "future" objects on this page,
433     where "future" means objects in which the field specified in ``date_field``
434     is greater than the current date/time. By default, this is ``False``.
435
436 Template name
437 ~~~~~~~~~~~~~
438
439 If ``template_name`` isn't specified, this view will use the template
440 ``<app_label>/<model_name>_archive_month.html`` by default.
441
442 Template context
443 ~~~~~~~~~~~~~~~~
444
445 In addition to ``extra_context``, the template's context will be:
446
447 ``month``
448     A ``datetime.date`` object representing the given month.
449
450 ``next_month``
451     A ``datetime.date`` object representing the first day of the next month. If
452      the next month is in the future, this will be ``None``.
453
454 ``previous_month``
455     A ``datetime.date`` object representing the first day of the previous
456     month. Unlike ``next_month``, this will never be ``None``.
457
458 ``object_list``
459     A list of objects available for the given month. This variable's name
460     depends on the ``template_object_name`` parameter, which is ``'object'`` by
461     default. If ``template_object_name`` is ``'foo'``, this variable's name
462     will be ``foo_list``.
463
464 .. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
465
466 ``django.views.generic.date_based.archive_week``
467 ------------------------------------------------
468
469 Description
470 ~~~~~~~~~~~
471
472 A weekly archive page showing all objects in a given week. Objects with a date
473 in the *future* are not displayed unless you set ``allow_future`` to ``True``.
474
475 Required arguments
476 ~~~~~~~~~~~~~~~~~~
477
478 ``year``
479     The four-digit year for which the archive serves (a string).
480
481 ``week``
482     The week of the year for which the archive serves (a string). Weeks start
483     with Sunday.
484
485 ``queryset``
486     A ``QuerySet`` of objects for which the archive serves.
487
488 ``date_field``
489     The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
490     model that the date-based archive should use to determine the objects on
491     the page.
492
493 Optional arguments
494 ~~~~~~~~~~~~~~~~~~
495
496 ``template_name``
497     The full name of a template to use in rendering the page. This lets you
498     override the default template name (see below).
499
500 ``template_loader``
501     The template loader to use when loading the template. By default, it's
502     ``django.template.loader``.
503
504 ``extra_context``
505     A dictionary of values to add to the template context. By default, this is
506     an empty dictionary. If a value in the dictionary is callable, the generic
507     view will call it just before rendering the template.
508
509 ``allow_empty``
510     A boolean specifying whether to display the page if no objects are
511     available. If this is ``False`` and no objects are available, the view will
512     raise a 404 instead of displaying an empty page. By default, this is
513     ``True``.
514
515 ``context_processors``
516     A list of template-context processors to apply to the view's template. See
517     the `RequestContext docs`_.
518
519 ``template_object_name``
520     Designates the name of the template variable to use in the template
521     context. By default, this is ``'object'``. The view will append ``'_list'``
522     to the value of this parameter in determining the variable's name.
523
524 ``mimetype``
525     The MIME type to use for the resulting document. Defaults to the value of
526     the ``DEFAULT_CONTENT_TYPE`` setting.
527
528 ``allow_future``
529     A boolean specifying whether to include "future" objects on this page,
530     where "future" means objects in which the field specified in ``date_field``
531     is greater than the current date/time. By default, this is ``False``.
532
533 Template name
534 ~~~~~~~~~~~~~
535
536 If ``template_name`` isn't specified, this view will use the template
537 ``<app_label>/<model_name>_archive_week.html`` by default.
538
539 Template context
540 ~~~~~~~~~~~~~~~~
541
542 In addition to ``extra_context``, the template's context will be:
543
544 ``week``
545     A ``datetime.date`` object representing the first day of the given week.
546
547 ``object_list``
548     A list of objects available for the given week. This variable's name
549     depends on the ``template_object_name`` parameter, which is ``'object'`` by
550     default. If ``template_object_name`` is ``'foo'``, this variable's name
551     will be ``foo_list``.
552
553 ``django.views.generic.date_based.archive_day``
554 -----------------------------------------------
555
556 Description
557 ~~~~~~~~~~~
558
559 A day archive page showing all objects in a given day. Days in the future throw
560 a 404 error, regardless of whether any objects exist for future days, unless
561 you set ``allow_future`` to ``True``.
562
563 Required arguments
564 ~~~~~~~~~~~~~~~~~~
565
566 ``year``
567     The four-digit year for which the archive serves (a string).
568
569 ``month``
570     The month for which the archive serves, formatted according to the
571     ``month_format`` argument.
572
573 ``day``
574     The day for which the archive serves, formatted according to the
575     ``day_format`` argument.
576
577 ``queryset``
578     A ``QuerySet`` of objects for which the archive serves.
579
580 ``date_field``
581     The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
582     model that the date-based archive should use to determine the objects on
583     the page.
584
585 Optional arguments
586 ~~~~~~~~~~~~~~~~~~
587
588 ``month_format``
589     A format string that regulates what format the ``month`` parameter uses.
590     This should be in the syntax accepted by Python's ``time.strftime``. (See
591     the `strftime docs`_.) It's set to ``"%b"`` by default, which is a three-
592     letter month abbreviation. To change it to use numbers, use ``"%m"``.
593
594 ``day_format``
595     Like ``month_format``, but for the ``day`` parameter. It defaults to
596     ``"%d"`` (day of the month as a decimal number, 01-31).
597
598 ``template_name``
599     The full name of a template to use in rendering the page. This lets you
600     override the default template name (see below).
601
602 ``template_loader``
603     The template loader to use when loading the template. By default, it's
604     ``django.template.loader``.
605
606 ``extra_context``
607     A dictionary of values to add to the template context. By default, this is
608     an empty dictionary. If a value in the dictionary is callable, the generic
609     view will call it just before rendering the template.
610
611 ``allow_empty``
612     A boolean specifying whether to display the page if no objects are
613     available. If this is ``False`` and no objects are available, the view will
614     raise a 404 instead of displaying an empty page. By default, this is
615     ``False``.
616
617 ``context_processors``
618     A list of template-context processors to apply to the view's template. See
619     the `RequestContext docs`_.
620
621 ``template_object_name``
622      Designates the name of the template variable to use in the template
623      context. By default, this is ``'object'``. The view will append
624      ``'_list'`` to the value of this parameter in determining the variable's
625      name.
626
627 ``mimetype``
628     The MIME type to use for the resulting document. Defaults to the value of
629     the ``DEFAULT_CONTENT_TYPE`` setting.
630
631 ``allow_future``
632     A boolean specifying whether to include "future" objects on this page,
633     where "future" means objects in which the field specified in ``date_field``
634     is greater than the current date/time. By default, this is ``False``.
635
636 Template name
637 ~~~~~~~~~~~~~
638
639 If ``template_name`` isn't specified, this view will use the template
640 ``<app_label>/<model_name>_archive_day.html`` by default.
641
642 Template context
643 ~~~~~~~~~~~~~~~~
644
645 In addition to ``extra_context``, the template's context will be:
646
647 ``day``
648     A ``datetime.date`` object representing the given day.
649
650 ``next_day``
651     A ``datetime.date`` object representing the next day. If the next day is in
652     the future, this will be ``None``.
653
654 ``previous_day``
655     A ``datetime.date`` object representing the given day. Unlike ``next_day``,
656     this will never be ``None``.
657
658 ``object_list``
659     A list of objects available for the given day. This variable's name depends
660     on the ``template_object_name`` parameter, which is ``'object'`` by
661     default. If ``template_object_name`` is ``'foo'``, this variable's name
662     will be ``foo_list``.
663
664 ``django.views.generic.date_based.archive_today``
665 -------------------------------------------------
666
667 Description
668 ~~~~~~~~~~~
669
670 A day archive page showing all objects for *today*. This is exactly the same as
671 ``archive_day``, except the ``year``/``month``/``day`` arguments are not used,
672 and today's date is used instead.
673
674 ``django.views.generic.date_based.object_detail``
675 -------------------------------------------------
676
677 Description
678 ~~~~~~~~~~~
679
680 A page representing an individual object. If the object has a date value in the
681 future, the view will throw a 404 error by default, unless you set
682 ``allow_future`` to ``True``.
683
684 Required arguments
685 ~~~~~~~~~~~~~~~~~~
686
687 ``year``
688     The object's four-digit year (a string).
689
690 ``month``
691     The object's month , formatted according to the ``month_format`` argument.
692
693 ``day``
694     The object's day , formatted according to the ``day_format`` argument.
695
696 ``queryset``
697     A ``QuerySet`` that contains the object.
698
699 ``date_field``
700     The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
701     model that the generic view should use to look up the object according to
702     ``year``, ``month`` and ``day``.
703
704 Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
705     If you provide ``object_id``, it should be the value of the primary-key
706     field for the object being displayed on this page.
707
708     Otherwise, ``slug`` should be the slug of the given object, and
709     ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
710     model. By default, ``slug_field`` is ``'slug'``.
711
712 Optional arguments
713 ~~~~~~~~~~~~~~~~~~
714
715 ``month_format``
716     A format string that regulates what format the ``month`` parameter uses.
717     This should be in the syntax accepted by Python's ``time.strftime``. (See
718     the `strftime docs`_.) It's set to ``"%b"`` by default, which is a three-
719     letter month abbreviation. To change it to use numbers, use ``"%m"``.
720
721 ``day_format``
722     Like ``month_format``, but for the ``day`` parameter. It defaults to
723     ``"%d"`` (day of the month as a decimal number, 01-31).
724
725 ``template_name``
726     The full name of a template to use in rendering the page. This lets you
727     override the default template name (see below).
728
729 ``template_name_field``
730     The name of a field on the object whose value is the template name to use.
731     This lets you store template names in the data. In other words, if your
732     object has a field ``'the_template'`` that contains a string
733     ``'foo.html'``, and you set ``template_name_field`` to ``'the_template'``,
734     then the generic view for this object will use the template ``'foo.html'``.
735
736     It's a bit of a brain-bender, but it's useful in some cases.
737
738 ``template_loader``
739     The template loader to use when loading the  template. By default, it's
740     ``django.template.loader``.
741
742 ``extra_context``
743     A dictionary of values to add to the template context. By default, this is
744     an empty dictionary. If a value in the dictionary is callable, the generic
745     view will call it just before rendering the template.
746
747 ``context_processors``
748     A list of template-context processors to apply to the view's template. See
749     the `RequestContext docs`_.
750
751 ``template_object_name``
752     Designates the name of the template variable to use in the template
753     context. By default, this is ``'object'``.
754
755 ``mimetype``
756     The MIME type to use for the resulting document. Defaults to the value of
757     the ``DEFAULT_CONTENT_TYPE`` setting.
758
759 ``allow_future``
760     A boolean specifying whether to include "future" objects on this page,
761     where "future" means objects in which the field specified in ``date_field``
762     is greater than the current date/time. By default, this is ``False``.
763
764 Template name
765 ~~~~~~~~~~~~~
766
767 If ``template_name`` isn't specified, this view will use the template
768 ``<app_label>/<model_name>_detail.html`` by default.
769
770 Template context
771 ~~~~~~~~~~~~~~~~
772
773 In addition to ``extra_context``, the template's context will be:
774
775 ``object``
776     The object. This variable's name depends on the ``template_object_name``
777     parameter, which is ``'object'`` by default. If ``template_object_name`` is
778     ``'foo'``, this variable's name will be ``foo``.
779
780 List/detail generic views
781 =========================
782
783 The list-detail generic-view framework (in the
784 ``django.views.generic.list_detail`` module) is similar to the date-based one,
785 except the former simply has two views: a list of objects and an individual
786 object page.
787
788 ``django.views.generic.list_detail.object_list``
789 ------------------------------------------------
790
791 Description
792 ~~~~~~~~~~~
793
794 A page representing a list of objects.
795
796 Required arguments
797 ~~~~~~~~~~~~~~~~~~
798
799 ``queryset``
800     A ``QuerySet`` that represents the objects.
801
802 Optional arguments
803 ~~~~~~~~~~~~~~~~~~
804
805 ``paginate_by``
806     An integer specifying how many objects should be displayed per page. If
807     this is given, the view will paginate objects with ``paginate_by`` objects
808     per page. The view will expect either a ``page`` query string parameter
809     (via ``GET``) or a ``page`` variable specified in the URLconf. See `Notes
810     on pagination`_ below.
811
812 ``page``
813     The current (1-based) page number, as an integer, or the string ``'last'``.
814     See `Notes on pagination`_ below.
815
816 ``template_name``
817     The full name of a template to use in rendering the page. This lets you
818     override the default template name (see below).
819
820 ``template_loader``
821     The template loader to use when loading the template. By default, it's
822     ``django.template.loader``.
823
824 ``extra_context``
825     A dictionary of values to add to the template context. By default, this is
826     an empty dictionary. If a value in the dictionary is callable, the generic
827     view will call it just before rendering the template.
828
829 ``allow_empty``
830     A boolean specifying whether to display the page if no objects are
831     available. If this is ``False`` and no objects are available, the view will
832     raise a 404 instead of displaying an empty page. By default, this is
833     ``True``.
834
835 ``context_processors``
836     A list of template-context processors to apply to the view's template. See
837     the `RequestContext docs`_.
838
839 ``template_object_name``
840     Designates the name of the template variable to use in the template
841     context. By default, this is ``'object'``. The view will append ``'_list'``
842     to the value of this parameter in determining the variable's name.
843
844 ``mimetype``
845     The MIME type to use for the resulting document. Defaults to the value of
846     the ``DEFAULT_CONTENT_TYPE`` setting.
847
848 Template name
849 ~~~~~~~~~~~~~
850
851 If ``template_name`` isn't specified, this view will use the template
852 ``<app_label>/<model_name>_list.html`` by default.
853
854 Template context
855 ~~~~~~~~~~~~~~~~
856
857 In addition to ``extra_context``, the template's context will be:
858
859 ``object_list``
860     The list of objects. This variable's name depends on the
861     ``template_object_name`` parameter, which is ``'object'`` by default. If
862     ``template_object_name`` is ``'foo'``, this variable's name will be
863     ``foo_list``.
864
865 ``is_paginated``
866     A boolean representing whether the results are paginated. Specifically,
867     this is set to ``False`` if the number of available objects is less than or
868     equal to ``paginate_by``.
869
870 If the results are paginated, the context will contain these extra variables:
871
872 ``paginator`` (**New in Django development version**)
873     An instance of ``django.core.paginator.Paginator``.
874
875 ``page_obj`` (**New in Django development version**)
876     An instance of ``django.core.paginator.Page``.
877
878 See the `pagination documentation`_ for more information on the ``Paginator``
879 and ``Page`` objects.
880
881 Notes on pagination
882 ~~~~~~~~~~~~~~~~~~~
883
884 If ``paginate_by`` is specified, Django will paginate the results. You can
885 specify the page number in the URL in one of two ways:
886
887     * Use the ``page`` parameter in the URLconf. For example, this is what
888       your URLconf might look like::
889
890         (r'^objects/page(?P<page>[0-9]+)/$', 'object_list', dict(info_dict))
891
892     * Pass the page number via the ``page`` query-string parameter. For
893       example, a URL would look like this::
894
895         /objects/?page=3
896
897     * To loop over all the available page numbers, use the ``page_range``
898       variable. You can iterate over the list provided by ``page_range``
899       to create a link to every page of results.
900
901 These values and lists are 1-based, not 0-based, so the first page would be
902 represented as page ``1``.
903
904 For more on pagination, read the `pagination documentation`_.
905
906 .. _`pagination documentation`: ../pagination/
907
908 **New in Django development version:**
909
910 As a special case, you are also permitted to use ``last`` as a value for
911 ``page``::
912
913     /objects/?page=last
914
915 This allows you to access the final page of results without first having to
916 determine how many pages there are.
917
918 Note that ``page`` *must* be either a valid page number or the value ``last``;
919 any other value for ``page`` will result in a 404 error.
920
921 ``django.views.generic.list_detail.object_detail``
922 --------------------------------------------------
923
924 A page representing an individual object.
925
926 Description
927 ~~~~~~~~~~~
928
929 A page representing an individual object.
930
931 Required arguments
932 ~~~~~~~~~~~~~~~~~~
933
934 ``queryset``
935     A ``QuerySet`` that contains the object.
936
937 Either ``object_id`` or (``slug`` *and* ``slug_field``)
938     If you provide ``object_id``, it should be the value of the primary-key
939     field for the object being displayed on this page.
940
941     Otherwise, ``slug`` should be the slug of the given object, and
942     ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
943     model. By default, ``slug_field`` is ``'slug'``.
944
945 Optional arguments
946 ~~~~~~~~~~~~~~~~~~
947
948 ``template_name``
949     The full name of a template to use in rendering the page. This lets you
950     override the default template name (see below).
951
952 ``template_name_field``
953     The name of a field on the object whose value is the template name to use.
954     This lets you store template names in the data. In other words, if your
955     object has a field ``'the_template'`` that contains a string
956     ``'foo.html'``, and you set ``template_name_field`` to ``'the_template'``,
957     then the generic view for this object will use the template ``'foo.html'``.
958
959     It's a bit of a brain-bender, but it's useful in some cases.
960
961 ``template_loader``
962     The template loader to use when loading the template. By default, it's
963     ``django.template.loader``.
964
965 ``extra_context``
966     A dictionary of values to add to the template context. By default, this is
967     an empty dictionary. If a value in the dictionary is callable, the generic
968     view will call it just before rendering the template.
969
970 ``context_processors``
971     A list of template-context processors to apply to the view's template. See
972     the `RequestContext docs`_.
973
974 ``template_object_name``
975     Designates the name of the template variable to use in the template
976     context. By default, this is ``'object'``.
977
978 ``mimetype``
979     The MIME type to use for the resulting document. Defaults to the value of
980     the ``DEFAULT_CONTENT_TYPE`` setting.
981
982 Template name
983 ~~~~~~~~~~~~~
984
985 If ``template_name`` isn't specified, this view will use the template
986 ``<app_label>/<model_name>_detail.html`` by default.
987
988 Template context
989 ~~~~~~~~~~~~~~~~
990
991 In addition to ``extra_context``, the template's context will be:
992
993 ``object``
994     The object. This variable's name depends on the ``template_object_name``
995     parameter, which is ``'object'`` by default. If ``template_object_name`` is
996     ``'foo'``, this variable's name will be ``foo``.
997
998 Create/update/delete generic views
999 ==================================
1000
1001 The ``django.views.generic.create_update`` module contains a set of functions
1002 for creating, editing and deleting objects.
1003
1004 **Changed in Django development version:**
1005
1006 ``django.views.generic.create_update.create_object`` and
1007 ``django.views.generic.create_update.update_object`` now use the new `forms
1008 library`_ to build and display the form.
1009
1010 .. _forms library: ../forms/
1011
1012 ``django.views.generic.create_update.create_object``
1013 ----------------------------------------------------
1014
1015 Description
1016 ~~~~~~~~~~~
1017
1018 A page that displays a form for creating an object, redisplaying the form with
1019 validation errors (if there are any) and saving the object.
1020
1021 Required arguments
1022 ~~~~~~~~~~~~~~~~~~
1023
1024 Either ``form_class`` or ``model``
1025     If you provide ``form_class``, it should be a ``django.forms.ModelForm``
1026     subclass.  Use this argument when you need to customize the model's form.
1027     See the `ModelForm docs`_ for more information.
1028
1029     Otherwise, ``model`` should be a Django model class and the form used will
1030     be a standard ``ModelForm`` for ``model``.
1031
1032 Optional arguments
1033 ~~~~~~~~~~~~~~~~~~
1034
1035 ``post_save_redirect``
1036     A URL to which the view will redirect after saving the object. By default,
1037     it's ``object.get_absolute_url()``.
1038
1039     ``post_save_redirect`` may contain dictionary string formatting, which will
1040     be interpolated against the object's field attributes. For example, you
1041     could use ``post_save_redirect="/polls/%(slug)s/"``.
1042
1043 ``login_required``
1044     A boolean that designates whether a user must be logged in, in order to see
1045     the page and save changes. This hooks into the Django `authentication
1046     system`_. By default, this is ``False``.
1047
1048     If this is ``True``, and a non-logged-in user attempts to visit this page
1049     or save the form, Django will redirect the request to ``/accounts/login/``.
1050
1051 ``template_name``
1052     The full name of a template to use in rendering the page. This lets you
1053     override the default template name (see below).
1054
1055 ``template_loader``
1056     The template loader to use when loading the template. By default, it's
1057     ``django.template.loader``.
1058
1059 ``extra_context``
1060     A dictionary of values to add to the template context. By default, this is
1061     an empty dictionary. If a value in the dictionary is callable, the generic
1062     view will call it just before rendering the template.
1063
1064 ``context_processors``
1065     A list of template-context processors to apply to the view's template. See
1066     the `RequestContext docs`_.
1067
1068 Template name
1069 ~~~~~~~~~~~~~
1070
1071 If ``template_name`` isn't specified, this view will use the template
1072 ``<app_label>/<model_name>_form.html`` by default.
1073
1074 Template context
1075 ~~~~~~~~~~~~~~~~
1076
1077 In addition to ``extra_context``, the template's context will be:
1078
1079 ``form``
1080     A ``django.forms.ModelForm`` instance representing the form for creating
1081     the object. This lets you refer to form fields easily in the template
1082     system.
1083
1084     For example, if the model has two fields, ``name`` and ``address``::
1085
1086         <form action="" method="post">
1087         <p>{{ form.name.label_tag }} {{ form.name }}</p>
1088         <p>{{ form.address.label_tag }} {{ form.address }}</p>
1089         </form>
1090
1091     See the `forms documentation`_ for more information about using ``Form``
1092     objects in templates.
1093
1094 .. _authentication system: ../authentication/
1095 .. _ModelForm docs: ../modelforms/
1096 .. _forms documentation: ../forms/
1097
1098 ``django.views.generic.create_update.update_object``
1099 ----------------------------------------------------
1100
1101 Description
1102 ~~~~~~~~~~~
1103
1104 A page that displays a form for editing an existing object, redisplaying the
1105 form with validation errors (if there are any) and saving changes to the
1106 object. This uses a form automatically generated from the object's
1107 model class.
1108
1109 Required arguments
1110 ~~~~~~~~~~~~~~~~~~
1111
1112 Either ``form_class`` or ``model``
1113     If you provide ``form_class``, it should be a ``django.forms.ModelForm``
1114     subclass.  Use this argument when you need to customize the model's form.
1115     See the `ModelForm docs`_ for more information.
1116
1117     Otherwise, ``model`` should be a Django model class and the form used will
1118     be a standard ``ModelForm`` for ``model``.
1119
1120 Either ``object_id`` or (``slug`` *and* ``slug_field``)
1121     If you provide ``object_id``, it should be the value of the primary-key
1122     field for the object being displayed on this page.
1123
1124     Otherwise, ``slug`` should be the slug of the given object, and
1125     ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
1126     model. By default, ``slug_field`` is ``'slug'``.
1127
1128 Optional arguments
1129 ~~~~~~~~~~~~~~~~~~
1130
1131 ``post_save_redirect``
1132     A URL to which the view will redirect after saving the object. By default,
1133     it's ``object.get_absolute_url()``.
1134
1135     ``post_save_redirect`` may contain dictionary string formatting, which will
1136     be interpolated against the object's field attributes. For example, you
1137     could use ``post_save_redirect="/polls/%(slug)s/"``.
1138
1139 ``login_required``
1140     A boolean that designates whether a user must be logged in, in order to see
1141     the page and save changes. This hooks into the Django `authentication
1142     system`_. By default, this is ``False``.
1143
1144     If this is ``True``, and a non-logged-in user attempts to visit this page
1145     or save the form, Django will redirect the request to ``/accounts/login/``.
1146
1147 ``template_name``
1148     The full name of a template to use in rendering the page. This lets you
1149     override the default template name (see below).
1150
1151 ``template_loader``
1152     The template loader to use when loading the template. By default, it's
1153     ``django.template.loader``.
1154
1155 ``extra_context``
1156     A dictionary of values to add to the template context. By default, this is
1157     an empty dictionary. If a value in the dictionary is callable, the generic
1158     view will call it just before rendering the template.
1159
1160 ``context_processors``: A list of template-context processors to apply to
1161       the view's template. See the `RequestContext docs`_.
1162
1163 ``template_object_name``
1164     Designates the name of the template variable to use in the template
1165     context. By default, this is ``'object'``.
1166
1167 Template name
1168 ~~~~~~~~~~~~~
1169
1170 If ``template_name`` isn't specified, this view will use the template
1171 ``<app_label>/<model_name>_form.html`` by default.
1172
1173 Template context
1174 ~~~~~~~~~~~~~~~~
1175
1176 In addition to ``extra_context``, the template's context will be:
1177
1178 ``form``
1179     A ``django.forms.ModelForm`` instance representing the form for editing the
1180     object. This lets you refer to form fields easily in the template system.
1181
1182     For example, if the model has two fields, ``name`` and ``address``::