Django

Code

root/django/branches/0.95-bugfixes/docs/generic_views.txt

Revision 3457, 43.0 kB (checked in by adrian, 2 years ago)

Fixed #2433 -- Added allow_future option to date-based generic views

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