generic-views.txt 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. .. _ref-generic-views:
  2. =============
  3. Generic views
  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. A general introduction to generic views can be found in the :ref:`topic guide
  10. <topics-generic-views>`.
  11. This reference contains details of Django's built-in generic views, along with
  12. a list of all keyword arguments that a generic view expects. Remember that
  13. arguments may either come from the URL pattern or from the ``extra_context``
  14. additional-information dictionary.
  15. Most generic views require the ``queryset`` key, which is a ``QuerySet``
  16. instance; see :ref:`topics-db-queries` for more information about ``QuerySet``
  17. objects.
  18. "Simple" generic views
  19. ======================
  20. The ``django.views.generic.simple`` module contains simple views to handle a
  21. couple of common cases: rendering a template when no view logic is needed,
  22. and issuing a redirect.
  23. ``django.views.generic.simple.direct_to_template``
  24. --------------------------------------------------
  25. **Description:**
  26. Renders a given template, passing it a ``{{ params }}`` template variable,
  27. which is a dictionary of the parameters captured in the URL.
  28. **Required arguments:**
  29. * ``template``: The full name of a template to use.
  30. **Optional arguments:**
  31. * ``extra_context``: A dictionary of values to add to the template
  32. context. By default, this is an empty dictionary. If a value in the
  33. dictionary is callable, the generic view will call it
  34. just before rendering the template.
  35. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  36. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  37. **Example:**
  38. Given the following URL patterns::
  39. urlpatterns = patterns('django.views.generic.simple',
  40. (r'^foo/$', 'direct_to_template', {'template': 'foo_index.html'}),
  41. (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
  42. )
  43. ... a request to ``/foo/`` would render the template ``foo_index.html``, and a
  44. request to ``/foo/15/`` would render the ``foo_detail.html`` with a context
  45. variable ``{{ params.id }}`` that is set to ``15``.
  46. ``django.views.generic.simple.redirect_to``
  47. -------------------------------------------
  48. **Description:**
  49. Redirects to a given URL.
  50. The given URL may contain dictionary-style string formatting, which will be
  51. interpolated against the parameters captured in the URL. Because keyword
  52. interpolation is *always* done (even if no arguments are passed in), any ``"%"``
  53. characters in the URL must be written as ``"%%"`` so that Python will convert
  54. them to a single percent sign on output.
  55. If the given URL is ``None``, Django will return an ``HttpResponseGone`` (410).
  56. **Required arguments:**
  57. * ``url``: The URL to redirect to, as a string. Or ``None`` to raise a 410
  58. (Gone) HTTP error.
  59. **Optional arguments:**
  60. * ``permanent``: Whether the redirect should be permanent. The only
  61. difference here is the HTTP status code returned. If ``True``, then the
  62. redirect will use status code 301. If ``False``, then the redirect will
  63. use status code 302. By default, ``permanent`` is ``True``.
  64. .. versionadded:: 1.1
  65. The ``permanent`` keyword argument is new in Django 1.1.
  66. **Example:**
  67. This example issues a permanent redirect (HTTP status code 301) from
  68. ``/foo/<id>/`` to ``/bar/<id>/``::
  69. urlpatterns = patterns('django.views.generic.simple',
  70. ('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}),
  71. )
  72. This example issues a non-permanent redirect (HTTP status code 302) from
  73. ``/foo/<id>/`` to ``/bar/<id>/``::
  74. urlpatterns = patterns('django.views.generic.simple',
  75. ('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/', 'permanent': False}),
  76. )
  77. This example returns a 410 HTTP error for requests to ``/bar/``::
  78. urlpatterns = patterns('django.views.generic.simple',
  79. ('^bar/$', 'redirect_to', {'url': None}),
  80. )
  81. This example shows how ``"%"`` characters must be written in the URL in order
  82. to avoid confusion with Python's string formatting markers. If the redirect
  83. string is written as ``"%7Ejacob/"`` (with only a single ``%``), an exception would be raised::
  84. urlpatterns = patterns('django.views.generic.simple',
  85. ('^bar/$', 'redirect_to', {'url': '%%7Ejacob.'}),
  86. )
  87. Date-based generic views
  88. ========================
  89. Date-based generic views (in the module ``django.views.generic.date_based``)
  90. are views for displaying drilldown pages for date-based data.
  91. ``django.views.generic.date_based.archive_index``
  92. -------------------------------------------------
  93. **Description:**
  94. A top-level index page showing the "latest" objects, by date. Objects with
  95. a date in the *future* are not included unless you set ``allow_future`` to
  96. ``True``.
  97. **Required arguments:**
  98. * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
  99. * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
  100. the ``QuerySet``'s model that the date-based archive should use to
  101. determine the objects on the page.
  102. **Optional arguments:**
  103. * ``num_latest``: The number of latest objects to send to the template
  104. context. By default, it's 15.
  105. * ``template_name``: The full name of a template to use in rendering the
  106. page. This lets you override the default template name (see below).
  107. * ``template_loader``: The template loader to use when loading the
  108. template. By default, it's ``django.template.loader``.
  109. * ``extra_context``: A dictionary of values to add to the template
  110. context. By default, this is an empty dictionary. If a value in the
  111. dictionary is callable, the generic view will call it
  112. just before rendering the template.
  113. * ``allow_empty``: A boolean specifying whether to display the page if no
  114. objects are available. If this is ``False`` and no objects are available,
  115. the view will raise a 404 instead of displaying an empty page. By
  116. default, this is ``True``.
  117. * ``context_processors``: A list of template-context processors to apply to
  118. the view's template.
  119. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  120. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  121. * ``allow_future``: A boolean specifying whether to include "future"
  122. objects on this page, where "future" means objects in which the field
  123. specified in ``date_field`` is greater than the current date/time. By
  124. default, this is ``False``.
  125. .. versionadded:: 1.0
  126. * ``template_object_name``: Designates the name of the template variable
  127. to use in the template context. By default, this is ``'latest'``.
  128. **Template name:**
  129. If ``template_name`` isn't specified, this view will use the template
  130. ``<app_label>/<model_name>_archive.html`` by default, where:
  131. * ``<model_name>`` is your model's name in all lowercase. For a model
  132. ``StaffMember``, that'd be ``staffmember``.
  133. * ``<app_label>`` is the right-most part of the full Python path to
  134. your model's app. For example, if your model lives in
  135. ``apps/blog/models.py``, that'd be ``blog``.
  136. **Template context:**
  137. In addition to ``extra_context``, the template's context will be:
  138. * ``date_list``: A list of ``datetime.date`` objects representing all
  139. years that have objects available according to ``queryset``. These are
  140. ordered in reverse. This is equivalent to
  141. ``queryset.dates(date_field, 'year')[::-1]``.
  142. .. versionchanged:: 1.0
  143. The behaviour depending on ``template_object_name`` is new in this version.
  144. * ``latest``: The ``num_latest`` objects in the system, ordered descending
  145. by ``date_field``. For example, if ``num_latest`` is ``10``, then
  146. ``latest`` will be a list of the latest 10 objects in ``queryset``.
  147. This variable's name depends on the ``template_object_name`` parameter,
  148. which is ``'latest'`` by default. If ``template_object_name`` is
  149. ``'foo'``, this variable's name will be ``foo``.
  150. ``django.views.generic.date_based.archive_year``
  151. ------------------------------------------------
  152. **Description:**
  153. A yearly archive page showing all available months in a given year. Objects
  154. with a date in the *future* are not displayed unless you set ``allow_future``
  155. to ``True``.
  156. **Required arguments:**
  157. * ``year``: The four-digit year for which the archive serves.
  158. * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
  159. * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
  160. the ``QuerySet``'s model that the date-based archive should use to
  161. determine the objects on the page.
  162. **Optional arguments:**
  163. * ``template_name``: The full name of a template to use in rendering the
  164. page. This lets you override the default template name (see below).
  165. * ``template_loader``: The template loader to use when loading the
  166. template. By default, it's ``django.template.loader``.
  167. * ``extra_context``: A dictionary of values to add to the template
  168. context. By default, this is an empty dictionary. If a value in the
  169. dictionary is callable, the generic view will call it
  170. just before rendering the template.
  171. * ``allow_empty``: A boolean specifying whether to display the page if no
  172. objects are available. If this is ``False`` and no objects are available,
  173. the view will raise a 404 instead of displaying an empty page. By
  174. default, this is ``False``.
  175. * ``context_processors``: A list of template-context processors to apply to
  176. the view's template.
  177. * ``template_object_name``: Designates the name of the template variable
  178. to use in the template context. By default, this is ``'object'``. The
  179. view will append ``'_list'`` to the value of this parameter in
  180. determining the variable's name.
  181. * ``make_object_list``: A boolean specifying whether to retrieve the full
  182. list of objects for this year and pass those to the template. If ``True``,
  183. this list of objects will be made available to the template as
  184. ``object_list``. (The name ``object_list`` may be different; see the docs
  185. for ``object_list`` in the "Template context" section below.) By default,
  186. this is ``False``.
  187. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  188. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  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. **Template name:**
  194. If ``template_name`` isn't specified, this view will use the template
  195. ``<app_label>/<model_name>_archive_year.html`` by default.
  196. **Template context:**
  197. In addition to ``extra_context``, the template's context will be:
  198. * ``date_list``: A list of ``datetime.date`` objects representing all
  199. months that have objects available in the given year, according to
  200. ``queryset``, in ascending order.
  201. * ``year``: The given year, as a four-character string.
  202. * ``object_list``: If the ``make_object_list`` parameter is ``True``, this
  203. will be set to a list of objects available for the given year, ordered by
  204. the date field. This variable's name depends on the
  205. ``template_object_name`` parameter, which is ``'object'`` by default. If
  206. ``template_object_name`` is ``'foo'``, this variable's name will be
  207. ``foo_list``.
  208. If ``make_object_list`` is ``False``, ``object_list`` will be passed to
  209. the template as an empty list.
  210. ``django.views.generic.date_based.archive_month``
  211. -------------------------------------------------
  212. **Description:**
  213. A monthly archive page showing all objects in a given month. Objects with a
  214. date in the *future* are not displayed unless you set ``allow_future`` to
  215. ``True``.
  216. **Required arguments:**
  217. * ``year``: The four-digit year for which the archive serves (a string).
  218. * ``month``: The month for which the archive serves, formatted according to
  219. the ``month_format`` argument.
  220. * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
  221. * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
  222. the ``QuerySet``'s model that the date-based archive should use to
  223. determine the objects on the page.
  224. **Optional arguments:**
  225. * ``month_format``: A format string that regulates what format the
  226. ``month`` parameter uses. This should be in the syntax accepted by
  227. Python's ``time.strftime``. (See the `strftime docs`_.) It's set to
  228. ``"%b"`` by default, which is a three-letter month abbreviation. To
  229. change it to use numbers, use ``"%m"``.
  230. * ``template_name``: The full name of a template to use in rendering the
  231. page. This lets you override the default template name (see below).
  232. * ``template_loader``: The template loader to use when loading the
  233. template. By default, it's ``django.template.loader``.
  234. * ``extra_context``: A dictionary of values to add to the template
  235. context. By default, this is an empty dictionary. If a value in the
  236. dictionary is callable, the generic view will call it
  237. just before rendering the template.
  238. * ``allow_empty``: A boolean specifying whether to display the page if no
  239. objects are available. If this is ``False`` and no objects are available,
  240. the view will raise a 404 instead of displaying an empty page. By
  241. default, this is ``False``.
  242. * ``context_processors``: A list of template-context processors to apply to
  243. the view's template.
  244. * ``template_object_name``: Designates the name of the template variable
  245. to use in the template context. By default, this is ``'object'``. The
  246. view will append ``'_list'`` to the value of this parameter in
  247. determining the variable's name.
  248. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  249. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  250. * ``allow_future``: A boolean specifying whether to include "future"
  251. objects on this page, where "future" means objects in which the field
  252. specified in ``date_field`` is greater than the current date/time. By
  253. default, this is ``False``.
  254. **Template name:**
  255. If ``template_name`` isn't specified, this view will use the template
  256. ``<app_label>/<model_name>_archive_month.html`` by default.
  257. **Template context:**
  258. In addition to ``extra_context``, the template's context will be:
  259. * ``month``: A ``datetime.date`` object representing the given month.
  260. * ``next_month``: A ``datetime.date`` object representing the first day of
  261. the next month. If the next month is in the future, this will be
  262. ``None``.
  263. * ``previous_month``: A ``datetime.date`` object representing the first day
  264. of the previous month. Unlike ``next_month``, this will never be
  265. ``None``.
  266. * ``object_list``: A list of objects available for the given month. This
  267. variable's name depends on the ``template_object_name`` parameter, which
  268. is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
  269. this variable's name will be ``foo_list``.
  270. .. _strftime docs: http://docs.python.org/library/time.html#time.strftime
  271. ``django.views.generic.date_based.archive_week``
  272. ------------------------------------------------
  273. **Description:**
  274. A weekly archive page showing all objects in a given week. Objects with a date
  275. in the *future* are not displayed unless you set ``allow_future`` to ``True``.
  276. **Required arguments:**
  277. * ``year``: The four-digit year for which the archive serves (a string).
  278. * ``week``: The week of the year for which the archive serves (a string).
  279. Weeks start with Sunday.
  280. * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
  281. * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
  282. the ``QuerySet``'s model that the date-based archive should use to
  283. determine the objects on the page.
  284. **Optional arguments:**
  285. * ``template_name``: The full name of a template to use in rendering the
  286. page. This lets you override the default template name (see below).
  287. * ``template_loader``: The template loader to use when loading the
  288. template. By default, it's ``django.template.loader``.
  289. * ``extra_context``: A dictionary of values to add to the template
  290. context. By default, this is an empty dictionary. If a value in the
  291. dictionary is callable, the generic view will call it
  292. just before rendering the template.
  293. * ``allow_empty``: A boolean specifying whether to display the page if no
  294. objects are available. If this is ``False`` and no objects are available,
  295. the view will raise a 404 instead of displaying an empty page. By
  296. default, this is ``True``.
  297. * ``context_processors``: A list of template-context processors to apply to
  298. the view's template.
  299. * ``template_object_name``: Designates the name of the template variable
  300. to use in the template context. By default, this is ``'object'``. The
  301. view will append ``'_list'`` to the value of this parameter in
  302. determining the variable's name.
  303. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  304. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  305. * ``allow_future``: A boolean specifying whether to include "future"
  306. objects on this page, where "future" means objects in which the field
  307. specified in ``date_field`` is greater than the current date/time. By
  308. default, this is ``False``.
  309. **Template name:**
  310. If ``template_name`` isn't specified, this view will use the template
  311. ``<app_label>/<model_name>_archive_week.html`` by default.
  312. **Template context:**
  313. In addition to ``extra_context``, the template's context will be:
  314. * ``week``: A ``datetime.date`` object representing the first day of the
  315. given week.
  316. * ``object_list``: A list of objects available for the given week. This
  317. variable's name depends on the ``template_object_name`` parameter, which
  318. is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
  319. this variable's name will be ``foo_list``.
  320. ``django.views.generic.date_based.archive_day``
  321. -----------------------------------------------
  322. **Description:**
  323. A day archive page showing all objects in a given day. Days in the future throw
  324. a 404 error, regardless of whether any objects exist for future days, unless
  325. you set ``allow_future`` to ``True``.
  326. **Required arguments:**
  327. * ``year``: The four-digit year for which the archive serves (a string).
  328. * ``month``: The month for which the archive serves, formatted according to
  329. the ``month_format`` argument.
  330. * ``day``: The day for which the archive serves, formatted according to the
  331. ``day_format`` argument.
  332. * ``queryset``: A ``QuerySet`` of objects for which the archive serves.
  333. * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
  334. the ``QuerySet``'s model that the date-based archive should use to
  335. determine the objects on the page.
  336. **Optional arguments:**
  337. * ``month_format``: A format string that regulates what format the
  338. ``month`` parameter uses. This should be in the syntax accepted by
  339. Python's ``time.strftime``. (See the `strftime docs`_.) It's set to
  340. ``"%b"`` by default, which is a three-letter month abbreviation. To
  341. change it to use numbers, use ``"%m"``.
  342. * ``day_format``: Like ``month_format``, but for the ``day`` parameter.
  343. It defaults to ``"%d"`` (day of the month as a decimal number, 01-31).
  344. * ``template_name``: The full name of a template to use in rendering the
  345. page. This lets you override the default template name (see below).
  346. * ``template_loader``: The template loader to use when loading the
  347. template. By default, it's ``django.template.loader``.
  348. * ``extra_context``: A dictionary of values to add to the template
  349. context. By default, this is an empty dictionary. If a value in the
  350. dictionary is callable, the generic view will call it
  351. just before rendering the template.
  352. * ``allow_empty``: A boolean specifying whether to display the page if no
  353. objects are available. If this is ``False`` and no objects are available,
  354. the view will raise a 404 instead of displaying an empty page. By
  355. default, this is ``False``.
  356. * ``context_processors``: A list of template-context processors to apply to
  357. the view's template.
  358. * ``template_object_name``: Designates the name of the template variable
  359. to use in the template context. By default, this is ``'object'``. The
  360. view will append ``'_list'`` to the value of this parameter in
  361. determining the variable's name.
  362. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  363. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  364. * ``allow_future``: A boolean specifying whether to include "future"
  365. objects on this page, where "future" means objects in which the field
  366. specified in ``date_field`` is greater than the current date/time. By
  367. default, this is ``False``.
  368. **Template name:**
  369. If ``template_name`` isn't specified, this view will use the template
  370. ``<app_label>/<model_name>_archive_day.html`` by default.
  371. **Template context:**
  372. In addition to ``extra_context``, the template's context will be:
  373. * ``day``: A ``datetime.date`` object representing the given day.
  374. * ``next_day``: A ``datetime.date`` object representing the next day. If
  375. the next day is in the future, this will be ``None``.
  376. * ``previous_day``: A ``datetime.date`` object representing the given day.
  377. Unlike ``next_day``, this will never be ``None``.
  378. * ``object_list``: A list of objects available for the given day. This
  379. variable's name depends on the ``template_object_name`` parameter, which
  380. is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
  381. this variable's name will be ``foo_list``.
  382. ``django.views.generic.date_based.archive_today``
  383. -------------------------------------------------
  384. **Description:**
  385. A day archive page showing all objects for *today*. This is exactly the same as
  386. ``archive_day``, except the ``year``/``month``/``day`` arguments are not used,
  387. and today's date is used instead.
  388. ``django.views.generic.date_based.object_detail``
  389. -------------------------------------------------
  390. **Description:**
  391. A page representing an individual object. If the object has a date value in the
  392. future, the view will throw a 404 error by default, unless you set
  393. ``allow_future`` to ``True``.
  394. **Required arguments:**
  395. * ``year``: The object's four-digit year (a string).
  396. * ``month``: The object's month , formatted according to the
  397. ``month_format`` argument.
  398. * ``day``: The object's day , formatted according to the ``day_format``
  399. argument.
  400. * ``queryset``: A ``QuerySet`` that contains the object.
  401. * ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in
  402. the ``QuerySet``'s model that the generic view should use to look up the
  403. object according to ``year``, ``month`` and ``day``.
  404. * Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
  405. If you provide ``object_id``, it should be the value of the primary-key
  406. field for the object being displayed on this page.
  407. Otherwise, ``slug`` should be the slug of the given object, and
  408. ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
  409. model. By default, ``slug_field`` is ``'slug'``.
  410. **Optional arguments:**
  411. * ``month_format``: A format string that regulates what format the
  412. ``month`` parameter uses. This should be in the syntax accepted by
  413. Python's ``time.strftime``. (See the `strftime docs`_.) It's set to
  414. ``"%b"`` by default, which is a three-letter month abbreviation. To
  415. change it to use numbers, use ``"%m"``.
  416. * ``day_format``: Like ``month_format``, but for the ``day`` parameter.
  417. It defaults to ``"%d"`` (day of the month as a decimal number, 01-31).
  418. * ``template_name``: The full name of a template to use in rendering the
  419. page. This lets you override the default template name (see below).
  420. * ``template_name_field``: The name of a field on the object whose value is
  421. the template name to use. This lets you store template names in the data.
  422. In other words, if your object has a field ``'the_template'`` that
  423. contains a string ``'foo.html'``, and you set ``template_name_field`` to
  424. ``'the_template'``, then the generic view for this object will use the
  425. template ``'foo.html'``.
  426. It's a bit of a brain-bender, but it's useful in some cases.
  427. * ``template_loader``: The template loader to use when loading the
  428. template. By default, it's ``django.template.loader``.
  429. * ``extra_context``: A dictionary of values to add to the template
  430. context. By default, this is an empty dictionary. If a value in the
  431. dictionary is callable, the generic view will call it
  432. just before rendering the template.
  433. * ``context_processors``: A list of template-context processors to apply to
  434. the view's template.
  435. * ``template_object_name``: Designates the name of the template variable
  436. to use in the template context. By default, this is ``'object'``.
  437. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  438. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  439. * ``allow_future``: A boolean specifying whether to include "future"
  440. objects on this page, where "future" means objects in which the field
  441. specified in ``date_field`` is greater than the current date/time. By
  442. default, this is ``False``.
  443. **Template name:**
  444. If ``template_name`` isn't specified, this view will use the template
  445. ``<app_label>/<model_name>_detail.html`` by default.
  446. **Template context:**
  447. In addition to ``extra_context``, the template's context will be:
  448. * ``object``: The object. This variable's name depends on the
  449. ``template_object_name`` parameter, which is ``'object'`` by default. If
  450. ``template_object_name`` is ``'foo'``, this variable's name will be
  451. ``foo``.
  452. List/detail generic views
  453. =========================
  454. The list-detail generic-view framework (in the
  455. ``django.views.generic.list_detail`` module) is similar to the date-based one,
  456. except the former simply has two views: a list of objects and an individual
  457. object page.
  458. ``django.views.generic.list_detail.object_list``
  459. ------------------------------------------------
  460. **Description:**
  461. A page representing a list of objects.
  462. **Required arguments:**
  463. * ``queryset``: A ``QuerySet`` that represents the objects.
  464. **Optional arguments:**
  465. * ``paginate_by``: An integer specifying how many objects should be
  466. displayed per page. If this is given, the view will paginate objects with
  467. ``paginate_by`` objects per page. The view will expect either a ``page``
  468. query string parameter (via ``GET``) or a ``page`` variable specified in
  469. the URLconf. See `Notes on pagination`_ below.
  470. * ``page``: The current page number, as an integer, or the string
  471. ``'last'``. This is 1-based. See `Notes on pagination`_ below.
  472. * ``template_name``: The full name of a template to use in rendering the
  473. page. This lets you override the default template name (see below).
  474. * ``template_loader``: The template loader to use when loading the
  475. template. By default, it's ``django.template.loader``.
  476. * ``extra_context``: A dictionary of values to add to the template
  477. context. By default, this is an empty dictionary. If a value in the
  478. dictionary is callable, the generic view will call it
  479. just before rendering the template.
  480. * ``allow_empty``: A boolean specifying whether to display the page if no
  481. objects are available. If this is ``False`` and no objects are available,
  482. the view will raise a 404 instead of displaying an empty page. By
  483. default, this is ``True``.
  484. * ``context_processors``: A list of template-context processors to apply to
  485. the view's template.
  486. * ``template_object_name``: Designates the name of the template variable
  487. to use in the template context. By default, this is ``'object'``. The
  488. view will append ``'_list'`` to the value of this parameter in
  489. determining the variable's name.
  490. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  491. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  492. **Template name:**
  493. If ``template_name`` isn't specified, this view will use the template
  494. ``<app_label>/<model_name>_list.html`` by default.
  495. **Template context:**
  496. .. versionadded:: 1.0
  497. The ``paginator`` and ``page_obj`` context variables are new.
  498. In addition to ``extra_context``, the template's context will be:
  499. * ``object_list``: The list of objects. This variable's name depends on the
  500. ``template_object_name`` parameter, which is ``'object'`` by default. If
  501. ``template_object_name`` is ``'foo'``, this variable's name will be
  502. ``foo_list``.
  503. * ``is_paginated``: A boolean representing whether the results are
  504. paginated. Specifically, this is set to ``False`` if the number of
  505. available objects is less than or equal to ``paginate_by``.
  506. If the results are paginated, the context will contain these extra variables:
  507. * ``paginator``: An instance of ``django.core.paginator.Paginator``.
  508. * ``page_obj``: An instance of ``django.core.paginator.Page``.
  509. Notes on pagination
  510. ~~~~~~~~~~~~~~~~~~~
  511. If ``paginate_by`` is specified, Django will paginate the results. You can
  512. specify the page number in the URL in one of two ways:
  513. * Use the ``page`` parameter in the URLconf. For example, this is what
  514. your URLconf might look like::
  515. (r'^objects/page(?P<page>[0-9]+)/$', 'object_list', dict(info_dict))
  516. * Pass the page number via the ``page`` query-string parameter. For
  517. example, a URL would look like this::
  518. /objects/?page=3
  519. * To loop over all the available page numbers, use the ``page_range``
  520. variable. You can iterate over the list provided by ``page_range``
  521. to create a link to every page of results.
  522. These values and lists are 1-based, not 0-based, so the first page would be
  523. represented as page ``1``.
  524. For more on pagination, read the :ref:`pagination documentation
  525. <topics-pagination>`.
  526. .. versionadded:: 1.0
  527. As a special case, you are also permitted to use ``last`` as a value for
  528. ``page``::
  529. /objects/?page=last
  530. This allows you to access the final page of results without first having to
  531. determine how many pages there are.
  532. Note that ``page`` *must* be either a valid page number or the value ``last``;
  533. any other value for ``page`` will result in a 404 error.
  534. ``django.views.generic.list_detail.object_detail``
  535. --------------------------------------------------
  536. A page representing an individual object.
  537. **Description:**
  538. A page representing an individual object.
  539. **Required arguments:**
  540. * ``queryset``: A ``QuerySet`` that contains the object.
  541. * Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
  542. If you provide ``object_id``, it should be the value of the primary-key
  543. field for the object being displayed on this page.
  544. Otherwise, ``slug`` should be the slug of the given object, and
  545. ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
  546. model. By default, ``slug_field`` is ``'slug'``.
  547. **Optional arguments:**
  548. * ``template_name``: The full name of a template to use in rendering the
  549. page. This lets you override the default template name (see below).
  550. * ``template_name_field``: The name of a field on the object whose value is
  551. the template name to use. This lets you store template names in the data.
  552. In other words, if your object has a field ``'the_template'`` that
  553. contains a string ``'foo.html'``, and you set ``template_name_field`` to
  554. ``'the_template'``, then the generic view for this object will use the
  555. template ``'foo.html'``.
  556. It's a bit of a brain-bender, but it's useful in some cases.
  557. * ``template_loader``: The template loader to use when loading the
  558. template. By default, it's ``django.template.loader``.
  559. * ``extra_context``: A dictionary of values to add to the template
  560. context. By default, this is an empty dictionary. If a value in the
  561. dictionary is callable, the generic view will call it
  562. just before rendering the template.
  563. * ``context_processors``: A list of template-context processors to apply to
  564. the view's template.
  565. * ``template_object_name``: Designates the name of the template variable
  566. to use in the template context. By default, this is ``'object'``.
  567. * ``mimetype``: The MIME type to use for the resulting document. Defaults
  568. to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
  569. **Template name:**
  570. If ``template_name`` isn't specified, this view will use the template
  571. ``<app_label>/<model_name>_detail.html`` by default.
  572. **Template context:**
  573. In addition to ``extra_context``, the template's context will be:
  574. * ``object``: The object. This variable's name depends on the
  575. ``template_object_name`` parameter, which is ``'object'`` by default. If
  576. ``template_object_name`` is ``'foo'``, this variable's name will be
  577. ``foo``.
  578. Create/update/delete generic views
  579. ==================================
  580. The ``django.views.generic.create_update`` module contains a set of functions
  581. for creating, editing and deleting objects.
  582. .. versionchanged:: 1.0
  583. ``django.views.generic.create_update.create_object`` and
  584. ``django.views.generic.create_update.update_object`` now use the new :ref:`forms
  585. library <topics-forms-index>` to build and display the form.
  586. ``django.views.generic.create_update.create_object``
  587. ----------------------------------------------------
  588. **Description:**
  589. A page that displays a form for creating an object, redisplaying the form with
  590. validation errors (if there are any) and saving the object.
  591. **Required arguments:**
  592. * Either ``form_class`` or ``model`` is required.
  593. If you provide ``form_class``, it should be a ``django.forms.ModelForm``
  594. subclass. Use this argument when you need to customize the model's form.
  595. See the :ref:`ModelForm docs <topics-forms-modelforms>` for more
  596. information.
  597. Otherwise, ``model`` should be a Django model class and the form used
  598. will be a standard ``ModelForm`` for ``model``.
  599. **Optional arguments:**
  600. * ``post_save_redirect``: A URL to which the view will redirect after
  601. saving the object. By default, it's ``object.get_absolute_url()``.
  602. ``post_save_redirect`` may contain dictionary string formatting, which
  603. will be interpolated against the object's field attributes. For example,
  604. you could use ``post_save_redirect="/polls/%(slug)s/"``.
  605. * ``login_required``: A boolean that designates whether a user must be
  606. logged in, in order to see the page and save changes. This hooks into the
  607. Django :ref:`authentication system <topics-auth>`. By default, this is
  608. ``False``.
  609. If this is ``True``, and a non-logged-in user attempts to visit this page
  610. or save the form, Django will redirect the request to ``/accounts/login/``.
  611. * ``template_name``: The full name of a template to use in rendering the
  612. page. This lets you override the default template name (see below).
  613. * ``template_loader``: The template loader to use when loading the
  614. template. By default, it's ``django.template.loader``.
  615. * ``extra_context``: A dictionary of values to add to the template
  616. context. By default, this is an empty dictionary. If a value in the
  617. dictionary is callable, the generic view will call it
  618. just before rendering the template.
  619. * ``context_processors``: A list of template-context processors to apply to
  620. the view's template.
  621. **Template name:**
  622. If ``template_name`` isn't specified, this view will use the template
  623. ``<app_label>/<model_name>_form.html`` by default.
  624. **Template context:**
  625. In addition to ``extra_context``, the template's context will be:
  626. * ``form``: A ``django.forms.ModelForm`` instance representing the form
  627. for creating the object. This lets you refer to form fields easily in the
  628. template system.
  629. For example, if the model has two fields, ``name`` and ``address``::
  630. <form action="" method="post">
  631. <p>{{ form.name.label_tag }} {{ form.name }}</p>
  632. <p>{{ form.address.label_tag }} {{ form.address }}</p>
  633. </form>
  634. See the :ref:`forms documentation <topics-forms-index>` for more
  635. information about using ``Form`` objects in templates.
  636. ``django.views.generic.create_update.update_object``
  637. ----------------------------------------------------
  638. **Description:**
  639. A page that displays a form for editing an existing object, redisplaying the
  640. form with validation errors (if there are any) and saving changes to the
  641. object. This uses a form automatically generated from the object's
  642. model class.
  643. **Required arguments:**
  644. * Either ``form_class`` or ``model`` is required.
  645. If you provide ``form_class``, it should be a ``django.forms.ModelForm``
  646. subclass. Use this argument when you need to customize the model's form.
  647. See the :ref:`ModelForm docs <topics-forms-modelforms>` for more
  648. information.
  649. Otherwise, ``model`` should be a Django model class and the form used
  650. will be a standard ``ModelForm`` for ``model``.
  651. * Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
  652. If you provide ``object_id``, it should be the value of the primary-key
  653. field for the object being displayed on this page.
  654. Otherwise, ``slug`` should be the slug of the given object, and
  655. ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
  656. model. By default, ``slug_field`` is ``'slug'``.
  657. **Optional arguments:**
  658. * ``post_save_redirect``: A URL to which the view will redirect after
  659. saving the object. By default, it's ``object.get_absolute_url()``.
  660. ``post_save_redirect`` may contain dictionary string formatting, which
  661. will be interpolated against the object's field attributes. For example,
  662. you could use ``post_save_redirect="/polls/%(slug)s/"``.
  663. * ``login_required``: A boolean that designates whether a user must be
  664. logged in, in order to see the page and save changes. This hooks into the
  665. Django :ref:`authentication system <topics-auth>`. By default, this is
  666. ``False``.
  667. If this is ``True``, and a non-logged-in user attempts to visit this page
  668. or save the form, Django will redirect the request to ``/accounts/login/``.
  669. * ``template_name``: The full name of a template to use in rendering the
  670. page. This lets you override the default template name (see below).
  671. * ``template_loader``: The template loader to use when loading the
  672. template. By default, it's ``django.template.loader``.
  673. * ``extra_context``: A dictionary of values to add to the template
  674. context. By default, this is an empty dictionary. If a value in the
  675. dictionary is callable, the generic view will call it
  676. just before rendering the template.
  677. * ``context_processors``: A list of template-context processors to apply to
  678. the view's template.
  679. * ``template_object_name``: Designates the name of the template variable
  680. to use in the template context. By default, this is ``'object'``.
  681. **Template name:**
  682. If ``template_name`` isn't specified, this view will use the template
  683. ``<app_label>/<model_name>_form.html`` by default.
  684. **Template context:**
  685. In addition to ``extra_context``, the template's context will be:
  686. * ``form``: A ``django.forms.ModelForm`` instance representing the form
  687. for editing the object. This lets you refer to form fields easily in the
  688. template system.
  689. For example, if the model has two fields, ``name`` and ``address``::
  690. <form action="" method="post">
  691. <p>{{ form.name.label_tag }} {{ form.name }}</p>
  692. <p>{{ form.address.label_tag }} {{ form.address }}</p>
  693. </form>
  694. See the :ref:`forms documentation <topics-forms-index>` for more
  695. information about using ``Form`` objects in templates.
  696. * ``object``: The original object being edited. This variable's name
  697. depends on the ``template_object_name`` parameter, which is ``'object'``
  698. by default. If ``template_object_name`` is ``'foo'``, this variable's
  699. name will be ``foo``.
  700. ``django.views.generic.create_update.delete_object``
  701. ----------------------------------------------------
  702. **Description:**
  703. A view that displays a confirmation page and deletes an existing object. The
  704. given object will only be deleted if the request method is ``POST``. If this
  705. view is fetched via ``GET``, it will display a confirmation page that should
  706. contain a form that POSTs to the same URL.
  707. **Required arguments:**
  708. * ``model``: The Django model class of the object that the form will
  709. create.
  710. * Either ``object_id`` or (``slug`` *and* ``slug_field``) is required.
  711. If you provide ``object_id``, it should be the value of the primary-key
  712. field for the object being displayed on this page.
  713. Otherwise, ``slug`` should be the slug of the given object, and
  714. ``slug_field`` should be the name of the slug field in the ``QuerySet``'s
  715. model. By default, ``slug_field`` is ``'slug'``.
  716. * ``post_delete_redirect``: A URL to which the view will redirect after
  717. deleting the object.
  718. **Optional arguments:**
  719. * ``login_required``: A boolean that designates whether a user must be
  720. logged in, in order to see the page and save changes. This hooks into the
  721. Django :ref:`authentication system <topics-auth>`. By default, this is
  722. ``False``.
  723. If this is ``True``, and a non-logged-in user attempts to visit this page
  724. or save the form, Django will redirect the request to ``/accounts/login/``.
  725. * ``template_name``: The full name of a template to use in rendering the
  726. page. This lets you override the default template name (see below).
  727. * ``template_loader``: The template loader to use when loading the
  728. template. By default, it's ``django.template.loader``.
  729. * ``extra_context``: A dictionary of values to add to the template
  730. context. By default, this is an empty dictionary. If a value in the
  731. dictionary is callable, the generic view will call it
  732. just before rendering the template.
  733. * ``context_processors``: A list of template-context processors to apply to
  734. the view's template.
  735. * ``template_object_name``: Designates the name of the template variable
  736. to use in the template context. By default, this is ``'object'``.
  737. **Template name:**
  738. If ``template_name`` isn't specified, this view will use the template
  739. ``<app_label>/<model_name>_confirm_delete.html`` by default.
  740. **Template context:**
  741. In addition to ``extra_context``, the template's context will be:
  742. * ``object``: The original object that's about to be deleted. This
  743. variable's name depends on the ``template_object_name`` parameter, which
  744. is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
  745. this variable's name will be ``foo``.