generic-views.txt 44 KB

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