Django

Code

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

Revision 4455, 43.2 kB (checked in by rmunn, 2 years ago)

Merged revisions 4186 to 4454 from trunk.

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