generic-views.txt 43 KB

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