generic-views.txt 44 KB

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