generic_views.txt 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. =============
  2. Generic views
  3. =============
  4. Writing Web applications can be monotonous, because we repeat certain patterns
  5. again and again. In Django, the most common of these patterns have been
  6. abstracted into "generic views" that let you quickly provide common views of
  7. an object without actually needing to write any Python code.
  8. Django's generic views contain the following:
  9. * A set of views for doing list/detail interfaces (for example,
  10. Django's `documentation index`_ and `detail pages`_).
  11. * A set of views for year/month/day archive pages and associated
  12. detail and "latest" pages (for example, the Django weblog's year_,
  13. month_, day_, detail_, and latest_ pages).
  14. * A set of views for creating, editing, and deleting objects.
  15. .. _`documentation index`: http://www.djangoproject.com/documentation/
  16. .. _`detail pages`: http://www.djangoproject.com/documentation/faq/
  17. .. _year: http://www.djangoproject.com/weblog/2005/
  18. .. _month: http://www.djangoproject.com/weblog/2005/jul/
  19. .. _day: http://www.djangoproject.com/weblog/2005/jul/20/
  20. .. _detail: http://www.djangoproject.com/weblog/2005/jul/20/autoreload/
  21. .. _latest: http://www.djangoproject.com/weblog/
  22. All of these views are used by creating configuration dictionaries in
  23. your URLconf files and passing those dictionaries as the third member of the
  24. URLconf tuple for a given pattern. For example, here's the URLconf for the
  25. simple weblog app that drives the blog on djangoproject.com::
  26. from django.conf.urls.defaults import *
  27. from django_website.apps.blog.models import Entry
  28. info_dict = {
  29. 'queryset': Entry.objects.all(),
  30. 'date_field': 'pub_date',
  31. }
  32. urlpatterns = patterns('django.views.generic.date_based',
  33. (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', info_dict),
  34. (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', info_dict),
  35. (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', info_dict),
  36. (r'^(?P<year>\d{4})/$', 'archive_year', info_dict),
  37. (r'^$', 'archive_index', info_dict),
  38. )
  39. As you can see, this URLconf defines a few options in ``info_dict``.
  40. ``'queryset'`` gives the generic view a ``QuerySet`` of objects to use (in this
  41. case, all of the ``Entry`` objects) and tells the generic view which model is
  42. being used.
  43. Documentation of each generic view follows, along with a list of all keyword
  44. arguments that a generic view expects. Remember that as in the example above,
  45. arguments may either come from the URL pattern (as ``month``, ``day``,
  46. ``year``, etc. do above) or from the additional-information dictionary (as for
  47. ``queryset``, ``date_field``, etc.).
  48. Most generic views require the ``queryset`` key, which is a ``QuerySet``
  49. instance; see the `database API docs`_ for more information about ``Queryset``
  50. objects.
  51. Most views also take an optional ``extra_context`` dictionary that you can use
  52. to pass any auxiliary information you wish to the view. The values in the
  53. ``extra_context`` dictionary can be either functions (or other callables) or
  54. other objects. Functions are evaluated just before they are passed to the
  55. template. However, note that QuerySets retrieve and cache their data when they
  56. are first evaluated, so if you want to pass in a QuerySet via
  57. ``extra_context`` that is always fresh you need to wrap it in a function or
  58. lambda that returns the QuerySet.
  59. .. _database API docs: ../db-api/
  60. "Simple" generic views
  61. ======================
  62. The ``django.views.generic.simple`` module contains simple views to handle a
  63. couple of common cases: rendering a template when no view logic is needed,
  64. and issuing a redirect.
  65. ``django.views.generic.simple.direct_to_template``
  66. --------------------------------------------------
  67. Description
  68. ~~~~~~~~~~~
  69. Renders a given template, passing it a ``{{ params }}`` template variable,
  70. which is a dictionary of the parameters captured in the URL.
  71. Required arguments
  72. ~~~~~~~~~~~~~~~~~~
  73. ``template``
  74. The full name of a template to use.
  75. Optional arguments
  76. ~~~~~~~~~~~~~~~~~~
  77. ``extra_context``
  78. A dictionary of values to add to the template context. By default, this is
  79. an empty dictionary. If a value in the dictionary is callable, the generic
  80. view will call it just before rendering the template.
  81. ``mimetype``
  82. The MIME type to use for the resulting document. Defaults to the value of
  83. the ``DEFAULT_CONTENT_TYPE`` setting.
  84. **Example:**
  85. Given the following URL patterns::
  86. urlpatterns = patterns('django.views.generic.simple',
  87. (r'^foo/$', 'direct_to_template', {'template': 'foo_index.html'}),
  88. (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
  89. )
  90. ... a request to ``/foo/`` would render the template ``foo_index.html``, and a
  91. request to ``/foo/15/`` would render the ``foo_detail.html`` with a context
  92. variable ``{{ params.id }}`` that is set to ``15``.
  93. ``django.views.generic.simple.redirect_to``
  94. -------------------------------------------
  95. Description
  96. ~~~~~~~~~~~
  97. Redirects to a given URL.
  98. The given URL may contain dictionary-style string formatting, which will be
  99. interpolated against the parameters captured in the URL.
  100. If the given URL is ``None``, Django will return an ``HttpResponseGone`` (410).
  101. Required arguments
  102. ~~~~~~~~~~~~~~~~~~
  103. ``url``
  104. The URL to redirect to, as a string. Or ``None`` to raise a 410 (Gone)
  105. HTTP error.
  106. **Example:**
  107. This example redirects from ``/foo/<id>/`` to ``/bar/<id>/``::
  108. urlpatterns = patterns('django.views.generic.simple',
  109. ('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}),
  110. )
  111. This example returns a 410 HTTP error for requests to ``/bar/``::
  112. urlpatterns = patterns('django.views.generic.simple',
  113. ('^bar/$', 'redirect_to', {'url': None}),
  114. )
  115. Date-based generic views
  116. ========================
  117. Date-based generic views (in the module ``django.views.generic.date_based``)
  118. are views for displaying drilldown pages for date-based data.
  119. ``django.views.generic.date_based.archive_index``
  120. -------------------------------------------------
  121. Description
  122. ~~~~~~~~~~~
  123. A top-level index page showing the "latest" objects, by date. Objects with
  124. a date in the *future* are not included unless you set ``allow_future`` to
  125. ``True``.
  126. Required arguments
  127. ~~~~~~~~~~~~~~~~~~
  128. ``queryset``
  129. A ``QuerySet`` of objects for which the archive serves.
  130. ``date_field``
  131. The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
  132. model that the date-based archive should use to determine the objects on
  133. the page.
  134. Optional arguments
  135. ~~~~~~~~~~~~~~~~~~
  136. ``num_latest``
  137. The number of latest objects to send to the template context. By default,
  138. it's 15.
  139. ``template_name``
  140. The full name of a template to use in rendering the page. This lets you
  141. override the default template name (see below).
  142. ``template_loader``
  143. The template loader to use when loading the template. By default, it's
  144. ``django.template.loader``.
  145. ``extra_context``
  146. A dictionary of values to add to the template context. By default, this is
  147. an empty dictionary. If a value in the dictionary is callable, the generic
  148. view will call it just before rendering the template.
  149. ``allow_empty``
  150. A boolean specifying whether to display the page if no objects are
  151. available. If this is ``False`` and no objects are available, the view will
  152. raise a 404 instead of displaying an empty page. By default, this is
  153. ``True``.
  154. ``context_processors``
  155. A list of template-context processors to apply to the view's template. See
  156. the `RequestContext docs`_.
  157. ``mimetype``
  158. The MIME type to use for the resulting document. Defaults to the value of
  159. the ``DEFAULT_CONTENT_TYPE`` setting.
  160. ``allow_future``
  161. A boolean specifying whether to include "future" objects on this page,
  162. where "future" means objects in which the field specified in ``date_field``
  163. is greater than the current date/time. By default, this is ``False``.
  164. ``template_object_name`` (**New in Django development version**)
  165. Designates the name of the template variable to use in the template
  166. context. By default, this is ``'latest'``.
  167. Template name
  168. ~~~~~~~~~~~~~
  169. If ``template_name`` isn't specified, this view will use the template
  170. ``<app_label>/<model_name>_archive.html`` by default, where:
  171. * ``<model_name>`` is your model's name in all lowercase. For a model
  172. ``StaffMember``, that'd be ``staffmember``.
  173. * ``<app_label>`` is the right-most part of the full Python path to
  174. your model's app. For example, if your model lives in
  175. ``apps/blog/models.py``, that'd be ``blog``.
  176. Template context
  177. ~~~~~~~~~~~~~~~~
  178. In addition to ``extra_context``, the template's context will be:
  179. ``date_list``
  180. A list of ``datetime.date`` objects representing all years that have
  181. objects available according to ``queryset``. These are ordered in reverse.
  182. This is equivalent to ``queryset.dates(date_field, 'year')[::-1]``.
  183. ``latest``
  184. The ``num_latest`` objects in the system, ordered descending by
  185. ``date_field``. For example, if ``num_latest`` is ``10``, then ``latest``
  186. will be a list of the latest 10 objects in ``queryset``.
  187. **New in Django development version:** This variable's name depends on the
  188. ``template_object_name`` parameter, which is ``'latest'`` by default.
  189. If ``template_object_name`` is ``'foo'``, this variable's name will be
  190. ``foo``.
  191. .. _RequestContext docs: ../templates_python/#subclassing-context-requestcontext
  192. ``django.views.generic.date_based.archive_year``
  193. ------------------------------------------------
  194. Description
  195. ~~~~~~~~~~~
  196. A yearly archive page showing all available months in a given year. Objects
  197. with a date in the *future* are not displayed unless you set ``allow_future``
  198. to ``True``.
  199. Required arguments
  200. ~~~~~~~~~~~~~~~~~~
  201. ``year``
  202. The four-digit year for which the archive serves.
  203. ``queryset``
  204. A ``QuerySet`` of objects for which the archive serves.
  205. ``date_field``
  206. The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
  207. model that the date-based archive should use to determine the objects on
  208. the page.
  209. Optional arguments
  210. ~~~~~~~~~~~~~~~~~~
  211. ``template_name``
  212. The full name of a template to use in rendering the page. This lets you
  213. override the default template name (see below).
  214. ``template_loader``
  215. The template loader to use when loading the template. By default, it's
  216. ``django.template.loader``.
  217. ``extra_context``
  218. A dictionary of values to add to the template context. By default, this is
  219. an empty dictionary. If a value in the dictionary is callable, the generic
  220. view will call it just before rendering the template.
  221. ``allow_empty``
  222. A boolean specifying whether to display the page if no objects are
  223. available. If this is ``False`` and no objects are available, the view will
  224. raise a 404 instead of displaying an empty page. By default, this is
  225. ``False``.
  226. ``context_processors``
  227. A list of template-context processors to apply to the view's template. See
  228. the `RequestContext docs`_.
  229. ``template_object_name``
  230. Designates the name of the template variable to use in the template
  231. context. By default, this is ``'object'``. The view will append ``'_list'``
  232. to the value of this parameter in determining the variable's name.
  233. ``make_object_list``
  234. A boolean specifying whether to retrieve the full list of objects for this
  235. year and pass those to the template. If ``True``, this list of objects will
  236. be made available to the template as ``object_list``. (The name
  237. ``object_list`` may be different; see the docs for ``object_list`` in the
  238. "Template context" section below.) By default, this is ``False``.
  239. ``mimetype``
  240. The MIME type to use for the resulting document. Defaults to the value of
  241. the ``DEFAULT_CONTENT_TYPE`` setting.
  242. ``allow_future``
  243. A boolean specifying whether to include "future" objects on this page,
  244. where "future" means objects in which the field specified in ``date_field``
  245. is greater than the current date/time. By default, this is ``False``.
  246. Template name
  247. ~~~~~~~~~~~~~
  248. If ``template_name`` isn't specified, this view will use the template
  249. ``<app_label>/<model_name>_archive_year.html`` by default.
  250. Template context
  251. ~~~~~~~~~~~~~~~~
  252. In addition to ``extra_context``, the template's context will be:
  253. ``date_list``
  254. A list of ``datetime.date`` objects representing all months that have
  255. objects available in the given year, according to ``queryset``, in
  256. ascending order.
  257. ``year``
  258. The given year, as a four-character string.
  259. ``object_list``
  260. If the ``make_object_list`` parameter is ``True``, this will be set to a
  261. list of objects available for the given year, ordered by the date field.
  262. This variable's name depends on the ``template_object_name`` parameter,
  263. which is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
  264. this variable's name will be ``foo_list``.
  265. If ``make_object_list`` is ``False``, ``object_list`` will be passed to the
  266. template as an empty list.
  267. ``django.views.generic.date_based.archive_month``
  268. -------------------------------------------------
  269. Description
  270. ~~~~~~~~~~~
  271. A monthly archive page showing all objects in a given month. Objects with a
  272. date in the *future* are not displayed unless you set ``allow_future`` to
  273. ``True``.
  274. Required arguments
  275. ~~~~~~~~~~~~~~~~~~
  276. ``year``
  277. The four-digit year for which the archive serves (a string).
  278. ``month``
  279. The month for which the archive serves, formatted according to the
  280. ``month_format`` argument.
  281. ``queryset``
  282. A ``QuerySet`` of objects for which the archive serves.
  283. ``date_field``
  284. The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
  285. model that the date-based archive should use to determine the objects on
  286. the page.
  287. Optional arguments
  288. ~~~~~~~~~~~~~~~~~~
  289. ``month_format``
  290. A format string that regulates what format the ``month`` parameter uses.
  291. This should be in the syntax accepted by Python's ``time.strftime``. (See
  292. the `strftime docs`_.) It's set to ``"%b"`` by default, which is a three-
  293. letter month abbreviation. To change it to use numbers, use ``"%m"``.
  294. ``template_name``
  295. The full name of a template to use in rendering the page. This lets you
  296. override the default template name (see below).
  297. ``template_loader``
  298. The template loader to use when loading the template. By default, it's
  299. ``django.template.loader``.
  300. ``extra_context``
  301. A dictionary of values to add to the template context. By default, this is
  302. an empty dictionary. If a value in the dictionary is callable, the generic
  303. view will call it just before rendering the template.
  304. ``allow_empty``
  305. A boolean specifying whether to display the page if no objects are
  306. available. If this is ``False`` and no objects are available, the view will
  307. raise a 404 instead of displaying an empty page. By default, this is
  308. ``False``.
  309. ``context_processors``
  310. A list of template-context processors to apply to the view's template. See
  311. the `RequestContext docs`_.
  312. ``template_object_name``
  313. Designates the name of the template variable to use in the template
  314. context. By default, this is ``'object'``. The view will append ``'_list'``
  315. to the value of this parameter in determining the variable's name.
  316. ``mimetype``
  317. The MIME type to use for the resulting document. Defaults to the value of
  318. the ``DEFAULT_CONTENT_TYPE`` setting.
  319. ``allow_future``
  320. A boolean specifying whether to include "future" objects on this page,
  321. where "future" means objects in which the field specified in ``date_field``
  322. is greater than the current date/time. By default, this is ``False``.
  323. Template name
  324. ~~~~~~~~~~~~~
  325. If ``template_name`` isn't specified, this view will use the template
  326. ``<app_label>/<model_name>_archive_month.html`` by default.
  327. Template context
  328. ~~~~~~~~~~~~~~~~
  329. In addition to ``extra_context``, the template's context will be:
  330. ``month``
  331. A ``datetime.date`` object representing the given month.
  332. ``next_month``
  333. A ``datetime.date`` object representing the first day of the next month. If
  334. the next month is in the future, this will be ``None``.
  335. ``previous_month``
  336. A ``datetime.date`` object representing the first day of the previous
  337. month. Unlike ``next_month``, this will never be ``None``.
  338. ``object_list``
  339. A list of objects available for the given month. This variable's name
  340. depends on the ``template_object_name`` parameter, which is ``'object'`` by
  341. default. If ``template_object_name`` is ``'foo'``, this variable's name
  342. will be ``foo_list``.
  343. .. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
  344. ``django.views.generic.date_based.archive_week``
  345. ------------------------------------------------
  346. Description
  347. ~~~~~~~~~~~
  348. A weekly archive page showing all objects in a given week. Objects with a date
  349. in the *future* are not displayed unless you set ``allow_future`` to ``True``.
  350. Required arguments
  351. ~~~~~~~~~~~~~~~~~~
  352. ``year``
  353. The four-digit year for which the archive serves (a string).
  354. ``week``
  355. The week of the year for which the archive serves (a string). Weeks start
  356. with Sunday.
  357. ``queryset``
  358. A ``QuerySet`` of objects for which the archive serves.
  359. ``date_field``
  360. The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
  361. model that the date-based archive should use to determine the objects on
  362. the page.
  363. Optional arguments
  364. ~~~~~~~~~~~~~~~~~~
  365. ``template_name``
  366. The full name of a template to use in rendering the page. This lets you
  367. override the default template name (see below).
  368. ``template_loader``
  369. The template loader to use when loading the template. By default, it's
  370. ``django.template.loader``.
  371. ``extra_context``
  372. A dictionary of values to add to the template context. By default, this is
  373. an empty dictionary. If a value in the dictionary is callable, the generic
  374. view will call it just before rendering the template.
  375. ``allow_empty``
  376. A boolean specifying whether to display the page if no objects are
  377. available. If this is ``False`` and no objects are available, the view will
  378. raise a 404 instead of displaying an empty page. By default, this is
  379. ``True``.
  380. ``context_processors``
  381. A list of template-context processors to apply to the view's template. See
  382. the `RequestContext docs`_.
  383. ``template_object_name``
  384. Designates the name of the template variable to use in the template
  385. context. By default, this is ``'object'``. The view will append ``'_list'``
  386. to the value of this parameter in determining the variable's name.
  387. ``mimetype``
  388. The MIME type to use for the resulting document. Defaults to the value of
  389. the ``DEFAULT_CONTENT_TYPE`` setting.
  390. ``allow_future``
  391. A boolean specifying whether to include "future" objects on this page,
  392. where "future" means objects in which the field specified in ``date_field``
  393. is greater than the current date/time. By default, this is ``False``.
  394. Template name
  395. ~~~~~~~~~~~~~
  396. If ``template_name`` isn't specified, this view will use the template
  397. ``<app_label>/<model_name>_archive_week.html`` by default.
  398. Template context
  399. ~~~~~~~~~~~~~~~~
  400. In addition to ``extra_context``, the template's context will be:
  401. ``week``
  402. A ``datetime.date`` object representing the first day of the given week.
  403. ``object_list``
  404. A list of objects available for the given week. This variable's name
  405. depends on the ``template_object_name`` parameter, which is ``'object'`` by
  406. default. If ``template_object_name`` is ``'foo'``, this variable's name
  407. will be ``foo_list``.
  408. ``django.views.generic.date_based.archive_day``
  409. -----------------------------------------------
  410. Description
  411. ~~~~~~~~~~~
  412. A day archive page showing all objects in a given day. Days in the future throw
  413. a 404 error, regardless of whether any objects exist for future days, unless
  414. you set ``allow_future`` to ``True``.
  415. Required arguments
  416. ~~~~~~~~~~~~~~~~~~
  417. ``year``
  418. The four-digit year for which the archive serves (a string).
  419. ``month``
  420. The month for which the archive serves, formatted according to the
  421. ``month_format`` argument.
  422. ``day``
  423. The day for which the archive serves, formatted according to the
  424. ``day_format`` argument.
  425. ``queryset``
  426. A ``QuerySet`` of objects for which the archive serves.
  427. ``date_field``
  428. The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
  429. model that the date-based archive should use to determine the objects on
  430. the page.
  431. Optional arguments
  432. ~~~~~~~~~~~~~~~~~~
  433. ``month_format``
  434. A format string that regulates what format the ``month`` parameter uses.
  435. This should be in the syntax accepted by Python's ``time.strftime``. (See
  436. the `strftime docs`_.) It's set to ``"%b"`` by default, which is a three-
  437. letter month abbreviation. To change it to use numbers, use ``"%m"``.
  438. ``day_format``
  439. Like ``month_format``, but for the ``day`` parameter. It defaults to
  440. ``"%d"`` (day of the month as a decimal number, 01-31).
  441. ``template_name``
  442. The full name of a template to use in rendering the page. This lets you
  443. override the default template name (see below).
  444. ``template_loader``
  445. The template loader to use when loading the template. By default, it's
  446. ``django.template.loader``.
  447. ``extra_context``
  448. A dictionary of values to add to the template context. By default, this is
  449. an empty dictionary. If a value in the dictionary is callable, the generic
  450. view will call it just before rendering the template.
  451. ``allow_empty``
  452. A boolean specifying whether to display the page if no objects are
  453. available. If this is ``False`` and no objects are available, the view will
  454. raise a 404 instead of displaying an empty page. By default, this is
  455. ``False``.
  456. ``context_processors``
  457. A list of template-context processors to apply to the view's template. See
  458. the `RequestContext docs`_.
  459. ``template_object_name``
  460. Designates the name of the template variable to use in the template
  461. context. By default, this is ``'object'``. The view will append
  462. ``'_list'`` to the value of this parameter in determining the variable's
  463. name.
  464. ``mimetype``
  465. The MIME type to use for the resulting document. Defaults to the value of
  466. the ``DEFAULT_CONTENT_TYPE`` setting.
  467. ``allow_future``
  468. A boolean specifying whether to include "future" objects on this page,
  469. where "future" means objects in which the field specified in ``date_field``
  470. is greater than the current date/time. By default, this is ``False``.
  471. Template name
  472. ~~~~~~~~~~~~~
  473. If ``template_name`` isn't specified, this view will use the template
  474. ``<app_label>/<model_name>_archive_day.html`` by default.
  475. Template context
  476. ~~~~~~~~~~~~~~~~
  477. In addition to ``extra_context``, the template's context will be:
  478. ``day``
  479. A ``datetime.date`` object representing the given day.
  480. ``next_day``
  481. A ``datetime.date`` object representing the next day. If the next day is in
  482. the future, this will be ``None``.
  483. ``previous_day``
  484. A ``datetime.date`` object representing the given day. Unlike ``next_day``,
  485. this will never be ``None``.
  486. ``object_list``
  487. A list of objects available for the given day. This variable's name depends
  488. on the ``template_object_name`` parameter, which is ``'object'`` by
  489. default. If ``template_object_name`` is ``'foo'``, this variable's name
  490. will be ``foo_list``.
  491. ``django.views.generic.date_based.archive_today``
  492. -------------------------------------------------
  493. Description
  494. ~~~~~~~~~~~
  495. A day archive page showing all objects for *today*. This is exactly the same as
  496. ``archive_day``, except the ``year``/``month``/``day`` arguments are not used,
  497. and today's date is used instead.
  498. ``django.views.generic.date_based.object_detail``
  499. -------------------------------------------------
  500. Description
  501. ~~~~~~~~~~~
  502. A page representing an individual object. If the object has a date value in the
  503. future, the view will throw a 404 error by default, unless you set
  504. ``allow_future`` to ``True``.
  505. Required arguments
  506. ~~~~~~~~~~~~~~~~~~
  507. ``year``
  508. The object's four-digit year (a string).
  509. ``month``
  510. The object's month , formatted according to the ``month_format`` argument.
  511. ``day``
  512. The object's day , formatted according to the ``day_format`` argument.
  513. ``queryset``
  514. A ``QuerySet`` that contains the object.
  515. ``date_field``
  516. The name of the ``DateField`` or ``DateTimeField`` in the ``QuerySet``'s
  517. model that the generic view should use to look up the object according to
  518. ``year``, ``month`` and ``day``.
  519. Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
  520. If you provide ``object_id``, it should be the value of the primary-key
  521. field for the object being displayed on this page.
  522. Otherwise, ``slug`` should be the slug of the given object, and
  523. ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
  524. model. By default, ``slug_field`` is ``'slug'``.
  525. Optional arguments
  526. ~~~~~~~~~~~~~~~~~~
  527. ``month_format``
  528. A format string that regulates what format the ``month`` parameter uses.
  529. This should be in the syntax accepted by Python's ``time.strftime``. (See
  530. the `strftime docs`_.) It's set to ``"%b"`` by default, which is a three-
  531. letter month abbreviation. To change it to use numbers, use ``"%m"``.
  532. ``day_format``
  533. Like ``month_format``, but for the ``day`` parameter. It defaults to
  534. ``"%d"`` (day of the month as a decimal number, 01-31).
  535. ``template_name``
  536. The full name of a template to use in rendering the page. This lets you
  537. override the default template name (see below).
  538. ``template_name_field``
  539. The name of a field on the object whose value is the template name to use.
  540. This lets you store template names in the data. In other words, if your
  541. object has a field ``'the_template'`` that contains a string
  542. ``'foo.html'``, and you set ``template_name_field`` to ``'the_template'``,
  543. then the generic view for this object will use the template ``'foo.html'``.
  544. It's a bit of a brain-bender, but it's useful in some cases.
  545. ``template_loader``
  546. The template loader to use when loading the template. By default, it's
  547. ``django.template.loader``.
  548. ``extra_context``
  549. A dictionary of values to add to the template context. By default, this is
  550. an empty dictionary. If a value in the dictionary is callable, the generic
  551. view will call it just before rendering the template.
  552. ``context_processors``
  553. A list of template-context processors to apply to the view's template. See
  554. the `RequestContext docs`_.
  555. ``template_object_name``
  556. Designates the name of the template variable to use in the template
  557. context. By default, this is ``'object'``.
  558. ``mimetype``
  559. The MIME type to use for the resulting document. Defaults to the value of
  560. the ``DEFAULT_CONTENT_TYPE`` setting.
  561. ``allow_future``
  562. A boolean specifying whether to include "future" objects on this page,
  563. where "future" means objects in which the field specified in ``date_field``
  564. is greater than the current date/time. By default, this is ``False``.
  565. Template name
  566. ~~~~~~~~~~~~~
  567. If ``template_name`` isn't specified, this view will use the template
  568. ``<app_label>/<model_name>_detail.html`` by default.
  569. Template context
  570. ~~~~~~~~~~~~~~~~
  571. In addition to ``extra_context``, the template's context will be:
  572. ``object``
  573. The object. This variable's name depends on the ``template_object_name``
  574. parameter, which is ``'object'`` by default. If ``template_object_name`` is
  575. ``'foo'``, this variable's name will be ``foo``.
  576. List/detail generic views
  577. =========================
  578. The list-detail generic-view framework (in the
  579. ``django.views.generic.list_detail`` module) is similar to the date-based one,
  580. except the former simply has two views: a list of objects and an individual
  581. object page.
  582. ``django.views.generic.list_detail.object_list``
  583. ------------------------------------------------
  584. Description
  585. ~~~~~~~~~~~
  586. A page representing a list of objects.
  587. Required arguments
  588. ~~~~~~~~~~~~~~~~~~
  589. ``queryset``
  590. A ``QuerySet`` that represents the objects.
  591. Optional arguments
  592. ~~~~~~~~~~~~~~~~~~
  593. ``paginate_by``
  594. An integer specifying how many objects should be displayed per page. If
  595. this is given, the view will paginate objects with ``paginate_by`` objects
  596. per page. The view will expect either a ``page`` query string parameter
  597. (via ``GET``) or a ``page`` variable specified in the URLconf. See `Notes
  598. on pagination`_ below.
  599. ``page``
  600. The current (1-based) page number, as an integer, or the string ``'last'``.
  601. See `Notes on pagination`_ below.
  602. ``template_name``
  603. The full name of a template to use in rendering the page. This lets you
  604. override the default template name (see below).
  605. ``template_loader``
  606. The template loader to use when loading the template. By default, it's
  607. ``django.template.loader``.
  608. ``extra_context``
  609. A dictionary of values to add to the template context. By default, this is
  610. an empty dictionary. If a value in the dictionary is callable, the generic
  611. view will call it just before rendering the template.
  612. ``allow_empty``
  613. A boolean specifying whether to display the page if no objects are
  614. available. If this is ``False`` and no objects are available, the view will
  615. raise a 404 instead of displaying an empty page. By default, this is
  616. ``True``.
  617. ``context_processors``
  618. A list of template-context processors to apply to the view's template. See
  619. the `RequestContext docs`_.
  620. ``template_object_name``
  621. Designates the name of the template variable to use in the template
  622. context. By default, this is ``'object'``. The view will append ``'_list'``
  623. to the value of this parameter in determining the variable's name.
  624. ``mimetype``
  625. The MIME type to use for the resulting document. Defaults to the value of
  626. the ``DEFAULT_CONTENT_TYPE`` setting.
  627. Template name
  628. ~~~~~~~~~~~~~
  629. If ``template_name`` isn't specified, this view will use the template
  630. ``<app_label>/<model_name>_list.html`` by default.
  631. Template context
  632. ~~~~~~~~~~~~~~~~
  633. In addition to ``extra_context``, the template's context will be:
  634. ``object_list``
  635. The list of objects. This variable's name depends on the
  636. ``template_object_name`` parameter, which is ``'object'`` by default. If
  637. ``template_object_name`` is ``'foo'``, this variable's name will be
  638. ``foo_list``.
  639. ``is_paginated``
  640. A boolean representing whether the results are paginated. Specifically,
  641. this is set to ``False`` if the number of available objects is less than or
  642. equal to ``paginate_by``.
  643. If the results are paginated, the context will contain these extra variables:
  644. ``paginator`` (**New in Django development version**)
  645. An instance of ``django.core.paginator.Paginator``.
  646. ``page_obj`` (**New in Django development version**)
  647. An instance of ``django.core.paginator.Page``.
  648. See the `pagination documentation`_ for more information on the ``Paginator``
  649. and ``Page`` objects.
  650. Notes on pagination
  651. ~~~~~~~~~~~~~~~~~~~
  652. If ``paginate_by`` is specified, Django will paginate the results. You can
  653. specify the page number in the URL in one of two ways:
  654. * Use the ``page`` parameter in the URLconf. For example, this is what
  655. your URLconf might look like::
  656. (r'^objects/page(?P<page>[0-9]+)/$', 'object_list', dict(info_dict))
  657. * Pass the page number via the ``page`` query-string parameter. For
  658. example, a URL would look like this::
  659. /objects/?page=3
  660. * To loop over all the available page numbers, use the ``page_range``
  661. variable. You can iterate over the list provided by ``page_range``
  662. to create a link to every page of results.
  663. These values and lists are 1-based, not 0-based, so the first page would be
  664. represented as page ``1``.
  665. For more on pagination, read the `pagination documentation`_.
  666. .. _`pagination documentation`: ../pagination/
  667. **New in Django development version:**
  668. As a special case, you are also permitted to use ``last`` as a value for
  669. ``page``::
  670. /objects/?page=last
  671. This allows you to access the final page of results without first having to
  672. determine how many pages there are.
  673. Note that ``page`` *must* be either a valid page number or the value ``last``;
  674. any other value for ``page`` will result in a 404 error.
  675. ``django.views.generic.list_detail.object_detail``
  676. --------------------------------------------------
  677. A page representing an individual object.
  678. Description
  679. ~~~~~~~~~~~
  680. A page representing an individual object.
  681. Required arguments
  682. ~~~~~~~~~~~~~~~~~~
  683. ``queryset``
  684. A ``QuerySet`` that contains the object.
  685. Either ``object_id`` or (``slug`` *and* ``slug_field``)
  686. If you provide ``object_id``, it should be the value of the primary-key
  687. field for the object being displayed on this page.
  688. Otherwise, ``slug`` should be the slug of the given object, and
  689. ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
  690. model. By default, ``slug_field`` is ``'slug'``.
  691. Optional arguments
  692. ~~~~~~~~~~~~~~~~~~
  693. ``template_name``
  694. The full name of a template to use in rendering the page. This lets you
  695. override the default template name (see below).
  696. ``template_name_field``
  697. The name of a field on the object whose value is the template name to use.
  698. This lets you store template names in the data. In other words, if your
  699. object has a field ``'the_template'`` that contains a string
  700. ``'foo.html'``, and you set ``template_name_field`` to ``'the_template'``,
  701. then the generic view for this object will use the template ``'foo.html'``.
  702. It's a bit of a brain-bender, but it's useful in some cases.
  703. ``template_loader``
  704. The template loader to use when loading the template. By default, it's
  705. ``django.template.loader``.
  706. ``extra_context``
  707. A dictionary of values to add to the template context. By default, this is
  708. an empty dictionary. If a value in the dictionary is callable, the generic
  709. view will call it just before rendering the template.
  710. ``context_processors``
  711. A list of template-context processors to apply to the view's template. See
  712. the `RequestContext docs`_.
  713. ``template_object_name``
  714. Designates the name of the template variable to use in the template
  715. context. By default, this is ``'object'``.
  716. ``mimetype``
  717. The MIME type to use for the resulting document. Defaults to the value of
  718. the ``DEFAULT_CONTENT_TYPE`` setting.
  719. Template name
  720. ~~~~~~~~~~~~~
  721. If ``template_name`` isn't specified, this view will use the template
  722. ``<app_label>/<model_name>_detail.html`` by default.
  723. Template context
  724. ~~~~~~~~~~~~~~~~
  725. In addition to ``extra_context``, the template's context will be:
  726. ``object``
  727. The object. This variable's name depends on the ``template_object_name``
  728. parameter, which is ``'object'`` by default. If ``template_object_name`` is
  729. ``'foo'``, this variable's name will be ``foo``.
  730. Create/update/delete generic views
  731. ==================================
  732. The ``django.views.generic.create_update`` module contains a set of functions
  733. for creating, editing and deleting objects.
  734. **Changed in Django development version:**
  735. ``django.views.generic.create_update.create_object`` and
  736. ``django.views.generic.create_update.update_object`` now use the new `forms
  737. library`_ to build and display the form.
  738. .. _forms library: ../forms/
  739. ``django.views.generic.create_update.create_object``
  740. ----------------------------------------------------
  741. Description
  742. ~~~~~~~~~~~
  743. A page that displays a form for creating an object, redisplaying the form with
  744. validation errors (if there are any) and saving the object.
  745. Required arguments
  746. ~~~~~~~~~~~~~~~~~~
  747. Either ``form_class`` or ``model``
  748. If you provide ``form_class``, it should be a ``django.forms.ModelForm``
  749. subclass. Use this argument when you need to customize the model's form.
  750. See the `ModelForm docs`_ for more information.
  751. Otherwise, ``model`` should be a Django model class and the form used will
  752. be a standard ``ModelForm`` for ``model``.
  753. Optional arguments
  754. ~~~~~~~~~~~~~~~~~~
  755. ``post_save_redirect``
  756. A URL to which the view will redirect after saving the object. By default,
  757. it's ``object.get_absolute_url()``.
  758. ``post_save_redirect`` may contain dictionary string formatting, which will
  759. be interpolated against the object's field attributes. For example, you
  760. could use ``post_save_redirect="/polls/%(slug)s/"``.
  761. ``login_required``
  762. A boolean that designates whether a user must be logged in, in order to see
  763. the page and save changes. This hooks into the Django `authentication
  764. system`_. By default, this is ``False``.
  765. If this is ``True``, and a non-logged-in user attempts to visit this page
  766. or save the form, Django will redirect the request to ``/accounts/login/``.
  767. ``template_name``
  768. The full name of a template to use in rendering the page. This lets you
  769. override the default template name (see below).
  770. ``template_loader``
  771. The template loader to use when loading the template. By default, it's
  772. ``django.template.loader``.
  773. ``extra_context``
  774. A dictionary of values to add to the template context. By default, this is
  775. an empty dictionary. If a value in the dictionary is callable, the generic
  776. view will call it just before rendering the template.
  777. ``context_processors``
  778. A list of template-context processors to apply to the view's template. See
  779. the `RequestContext docs`_.
  780. Template name
  781. ~~~~~~~~~~~~~
  782. If ``template_name`` isn't specified, this view will use the template
  783. ``<app_label>/<model_name>_form.html`` by default.
  784. Template context
  785. ~~~~~~~~~~~~~~~~
  786. In addition to ``extra_context``, the template's context will be:
  787. ``form``
  788. A ``django.forms.ModelForm`` instance representing the form for creating
  789. the object. This lets you refer to form fields easily in the template
  790. system.
  791. For example, if the model has two fields, ``name`` and ``address``::
  792. <form action="" method="post">
  793. <p>{{ form.name.label_tag }} {{ form.name }}</p>
  794. <p>{{ form.address.label_tag }} {{ form.address }}</p>
  795. </form>
  796. See the `forms documentation`_ for more information about using ``Form``
  797. objects in templates.
  798. .. _authentication system: ../authentication/
  799. .. _ModelForm docs: ../modelforms/
  800. .. _forms documentation: ../forms/
  801. ``django.views.generic.create_update.update_object``
  802. ----------------------------------------------------
  803. Description
  804. ~~~~~~~~~~~
  805. A page that displays a form for editing an existing object, redisplaying the
  806. form with validation errors (if there are any) and saving changes to the
  807. object. This uses a form automatically generated from the object's
  808. model class.
  809. Required arguments
  810. ~~~~~~~~~~~~~~~~~~
  811. Either ``form_class`` or ``model``
  812. If you provide ``form_class``, it should be a ``django.forms.ModelForm``
  813. subclass. Use this argument when you need to customize the model's form.
  814. See the `ModelForm docs`_ for more information.
  815. Otherwise, ``model`` should be a Django model class and the form used will
  816. be a standard ``ModelForm`` for ``model``.
  817. Either ``object_id`` or (``slug`` *and* ``slug_field``)
  818. If you provide ``object_id``, it should be the value of the primary-key
  819. field for the object being displayed on this page.
  820. Otherwise, ``slug`` should be the slug of the given object, and
  821. ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
  822. model. By default, ``slug_field`` is ``'slug'``.
  823. Optional arguments
  824. ~~~~~~~~~~~~~~~~~~
  825. ``post_save_redirect``
  826. A URL to which the view will redirect after saving the object. By default,
  827. it's ``object.get_absolute_url()``.
  828. ``post_save_redirect`` may contain dictionary string formatting, which will
  829. be interpolated against the object's field attributes. For example, you
  830. could use ``post_save_redirect="/polls/%(slug)s/"``.
  831. ``login_required``
  832. A boolean that designates whether a user must be logged in, in order to see
  833. the page and save changes. This hooks into the Django `authentication
  834. system`_. By default, this is ``False``.
  835. If this is ``True``, and a non-logged-in user attempts to visit this page
  836. or save the form, Django will redirect the request to ``/accounts/login/``.
  837. ``template_name``
  838. The full name of a template to use in rendering the page. This lets you
  839. override the default template name (see below).
  840. ``template_loader``
  841. The template loader to use when loading the template. By default, it's
  842. ``django.template.loader``.
  843. ``extra_context``
  844. A dictionary of values to add to the template context. By default, this is
  845. an empty dictionary. If a value in the dictionary is callable, the generic
  846. view will call it just before rendering the template.
  847. ``context_processors``: A list of template-context processors to apply to
  848. the view's template. See the `RequestContext docs`_.
  849. ``template_object_name``
  850. Designates the name of the template variable to use in the template
  851. context. By default, this is ``'object'``.
  852. Template name
  853. ~~~~~~~~~~~~~
  854. If ``template_name`` isn't specified, this view will use the template
  855. ``<app_label>/<model_name>_form.html`` by default.
  856. Template context
  857. ~~~~~~~~~~~~~~~~
  858. In addition to ``extra_context``, the template's context will be:
  859. ``form``
  860. A ``django.forms.ModelForm`` instance representing the form for editing the
  861. object. This lets you refer to form fields easily in the template system.
  862. For example, if the model has two fields, ``name`` and ``address``::
  863. <form action="" method="post">
  864. <p>{{ form.name.label_tag }} {{ form.name }}</p>
  865. <p>{{ form.address.label_tag }} {{ form.address }}</p>
  866. </form>
  867. See the `forms documentation`_ for more information about using ``Form``
  868. objects in templates.
  869. ``object``
  870. The original object being edited. This variable's name depends on the
  871. ``template_object_name`` parameter, which is ``'object'`` by default. If
  872. ``template_object_name`` is ``'foo'``, this variable's name will be
  873. ``foo``.
  874. ``django.views.generic.create_update.delete_object``
  875. ----------------------------------------------------
  876. Description
  877. ~~~~~~~~~~~
  878. A view that displays a confirmation page and deletes an existing object. The
  879. given object will only be deleted if the request method is ``POST``. If this
  880. view is fetched via ``GET``, it will display a confirmation page that should
  881. contain a form that POSTs to the same URL.
  882. Required arguments
  883. ~~~~~~~~~~~~~~~~~~
  884. ``model``
  885. The Django model class of the object that the form will create.
  886. Either ``object_id`` or (``slug`` *and* ``slug_field``)
  887. If you provide ``object_id``, it should be the value of the primary-key
  888. field for the object being displayed on this page.
  889. Otherwise, ``slug`` should be the slug of the given object, and
  890. ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
  891. model. By default, ``slug_field`` is ``'slug'``.
  892. ``post_delete_redirect``
  893. A URL to which the view will redirect after deleting the object.
  894. Optional arguments
  895. ~~~~~~~~~~~~~~~~~~
  896. ``login_required``
  897. A boolean that designates whether a user must be logged in, in order to see
  898. the page and save changes. This hooks into the Django `authentication
  899. system`_. By default, this is ``False``.
  900. If this is ``True``, and a non-logged-in user attempts to visit this page
  901. or save the form, Django will redirect the request to ``/accounts/login/``.
  902. ``template_name``
  903. The full name of a template to use in rendering the page. This lets you
  904. override the default template name (see below).
  905. ``template_loader``
  906. The template loader to use when loading the template. By default, it's
  907. ``django.template.loader``.
  908. ``extra_context``
  909. A dictionary of values to add to the template context. By default, this is
  910. an empty dictionary. If a value in the dictionary is callable, the generic
  911. view will call it just before rendering the template.
  912. ``context_processors``
  913. A list of template-context processors to apply to the view's template. See
  914. the `RequestContext docs`_.
  915. ``template_object_name``
  916. Designates the name of the template variable to use in the template
  917. context. By default, this is ``'object'``.
  918. Template name
  919. ~~~~~~~~~~~~~
  920. If ``template_name`` isn't specified, this view will use the template
  921. ``<app_label>/<model_name>_confirm_delete.html`` by default.
  922. Template context
  923. ~~~~~~~~~~~~~~~~
  924. In addition to ``extra_context``, the template's context will be:
  925. ``object``
  926. The original object that's about to be deleted. This variable's name
  927. depends on the ``template_object_name`` parameter, which is ``'object'`` by
  928. default. If ``template_object_name`` is ``'foo'``, this variable's name
  929. will be ``foo``.