2005-07-25 06:21:09 +08:00
|
|
|
===================
|
|
|
|
Using generic views
|
|
|
|
===================
|
|
|
|
|
2005-07-26 01:18:39 +08:00
|
|
|
Writing Web applications can be monotonous, because we repeat certain patterns
|
|
|
|
again and again. In Django, the most common of these patterns have been
|
|
|
|
abstracted into "generic views" that let you quickly provide common views of
|
|
|
|
an object without actually needing to write any views.
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
Django's generic views contain the following:
|
|
|
|
|
2005-07-26 01:18:39 +08:00
|
|
|
* A set of views for doing list/detail interfaces (for example,
|
2005-07-25 06:21:09 +08:00
|
|
|
Django's `documentation index`_ and `detail pages`_).
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
* A set of views for year/month/day archive pages and associated
|
|
|
|
detail and "latest" pages (for example, the Django weblog's year_,
|
|
|
|
month_, day_, detail_, and latest_ pages).
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
* A set of views for creating, editing, and deleting objects.
|
|
|
|
|
|
|
|
.. _`documentation index`: http://www.djangoproject.com/documentation/
|
|
|
|
.. _`detail pages`: http://www.djangoproject.com/documentation/faq/
|
|
|
|
.. _year: http://www.djangoproject.com/weblog/2005/
|
|
|
|
.. _month: http://www.djangoproject.com/weblog/2005/jul/
|
|
|
|
.. _day: http://www.djangoproject.com/weblog/2005/jul/20/
|
|
|
|
.. _detail: http://www.djangoproject.com/weblog/2005/jul/20/autoreload/
|
|
|
|
.. _latest: http://www.djangoproject.com/weblog/
|
2005-07-26 01:18:39 +08:00
|
|
|
|
|
|
|
All of these views are used by creating configuration dictionaries in
|
|
|
|
your URLconf files and passing those dictionaries as the third member of the
|
|
|
|
URLconf tuple. For example, here's the URLconf for the simple weblog app that
|
|
|
|
drives the blog on djangoproject.com::
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
from django.conf.urls.defaults import *
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
info_dict = {
|
2005-07-26 01:18:39 +08:00
|
|
|
'app_label': 'blog',
|
2005-07-25 06:21:09 +08:00
|
|
|
'module_name': 'entries',
|
2005-07-26 01:18:39 +08:00
|
|
|
'date_field': 'pub_date',
|
2005-07-25 06:21:09 +08:00
|
|
|
}
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
urlpatterns = patterns('django.views.generic.date_based',
|
|
|
|
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>\w+)/$', 'object_detail', dict(info_dict, slug_field='slug')),
|
|
|
|
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', info_dict),
|
|
|
|
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', info_dict),
|
|
|
|
(r'^(?P<year>\d{4})/$', 'archive_year', info_dict),
|
|
|
|
(r'^/?$', 'archive_index', info_dict),
|
|
|
|
)
|
|
|
|
|
2005-07-26 01:18:39 +08:00
|
|
|
As you can see, this URLconf defines a few options in ``info_dict`` that tell
|
2005-07-25 06:21:09 +08:00
|
|
|
the generic view which model to use (``blog.entries`` in this case), as well as
|
|
|
|
some extra information.
|
|
|
|
|
2005-07-26 01:18:39 +08:00
|
|
|
Documentation of each generic view follows, along with a list of all keyword
|
|
|
|
arguments that a generic view expects. Remember that as in the example above,
|
|
|
|
arguments may either come from the URL pattern (as ``month``, ``day``,
|
|
|
|
``year``, etc. do above) or from the additional-information dictionary (as for
|
|
|
|
``app_label``, ``module_name``, etc.).
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
All the generic views that follow require the ``app_label`` and ``module_name`` keys.
|
|
|
|
These values are easiest to explain through example::
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
>>> from django.models.blog import entries
|
2005-07-26 01:18:39 +08:00
|
|
|
|
|
|
|
In the above line, ``blog`` is the ``app_label`` (the name of the file that
|
|
|
|
holds all your model definitions) and ``entries`` is the ``module_name``
|
|
|
|
(either a pluralized, lowercased version of the model class name, or the value
|
|
|
|
of the ``module_name`` option of your model). In the docs below, these keys
|
|
|
|
will not be repeated, but each generic view requires them.
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
Using date-based generic views
|
|
|
|
==============================
|
|
|
|
|
|
|
|
Date-based generic views (in the module ``django.views.generic.date_based``)
|
2005-07-26 01:18:39 +08:00
|
|
|
feature six functions for dealing with date-based data. Besides ``app_label``
|
|
|
|
and ``module_name``, all date-based generic views require that the
|
|
|
|
``date_field`` argument be passed to them. This is the name of the field that
|
|
|
|
stores the date the objects should key off of.
|
2005-07-25 06:21:09 +08:00
|
|
|
|
2005-07-26 01:18:39 +08:00
|
|
|
Additionally, all date-based generic views have the following optional
|
|
|
|
arguments:
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
======================= ==================================================
|
|
|
|
Argument Description
|
|
|
|
======================= ==================================================
|
2005-07-26 01:18:39 +08:00
|
|
|
``template_name`` Overrides the default template name used for the
|
2005-07-25 06:21:09 +08:00
|
|
|
view.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
``extra_lookup_kwargs`` A dictionary of extra lookup parameters (see
|
|
|
|
the `database API docs`_).
|
2005-07-26 01:18:39 +08:00
|
|
|
|
|
|
|
``extra_context`` A dictionary of extra data to put into the
|
|
|
|
template's context.
|
2005-07-25 06:21:09 +08:00
|
|
|
======================= ==================================================
|
|
|
|
|
|
|
|
.. _`database API docs`: http://www.djangoproject.com/documentation/db_api/
|
|
|
|
|
|
|
|
The date-based generic functions are:
|
|
|
|
|
|
|
|
``archive_index``
|
2005-07-26 01:18:39 +08:00
|
|
|
A top-level index page showing the "latest" objects. Has an optional
|
|
|
|
argument, ``num_latest``, which is the number of items to display on the
|
|
|
|
page (defaults to 15).
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Uses the template ``app_label/module_name_archive`` by default.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Has the following template context:
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
``date_list``
|
|
|
|
List of years with objects
|
|
|
|
``latest``
|
|
|
|
Latest objects by date
|
|
|
|
|
|
|
|
``archive_year``
|
2005-07-26 01:18:39 +08:00
|
|
|
Yearly archive. Requires that the ``year`` argument be present in the URL
|
2005-07-25 06:21:09 +08:00
|
|
|
pattern.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Uses the template ``app_label/module_name__archive_year`` by default.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Has the following template context:
|
|
|
|
|
|
|
|
``date_list``
|
2005-07-26 01:18:39 +08:00
|
|
|
List of months in the given year with objects
|
2005-07-25 06:21:09 +08:00
|
|
|
``year``
|
2005-07-26 01:18:39 +08:00
|
|
|
The given year (an integer)
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
``archive_month``
|
2005-07-26 01:18:39 +08:00
|
|
|
Monthly archive. Requires that ``year`` and ``month`` arguments be given.
|
2005-07-26 11:48:41 +08:00
|
|
|
You can pass the additional option ``month_format`` if you'd like to change
|
|
|
|
the way months are specified in the URL.
|
|
|
|
|
|
|
|
``month_format`` is a format string in the same syntax accepted by Python's
|
|
|
|
``time.strftime``. (See the `strftime docs`_.) It's set to ``"%b"`` by
|
|
|
|
default, which is a three-letter month abbreviation. To change it to use
|
|
|
|
numbers, use ``"%m"``.
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Uses the template ``app_label/module_name__archive_month`` by default.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Has the following template context:
|
|
|
|
|
|
|
|
``month``
|
2005-07-26 01:18:39 +08:00
|
|
|
The given month (a datetime.datetime object)
|
2005-07-25 06:21:09 +08:00
|
|
|
``object_list``
|
2005-07-26 01:18:39 +08:00
|
|
|
List of objects published in the given month
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
``archive_day``
|
2005-07-26 01:18:39 +08:00
|
|
|
Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be
|
|
|
|
given.
|
|
|
|
|
2005-07-26 11:48:41 +08:00
|
|
|
As in ``archive_month``, you can pass an optional ``month_format``. You can
|
|
|
|
also pass ``day_format``, which defaults to ``"%d"`` (day of the month as a
|
|
|
|
decimal number, 1-31).
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Uses the template ``app_label/module_name__archive_day`` by default.
|
|
|
|
|
|
|
|
Has the following template context:
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
``object_list``
|
2005-07-26 01:18:39 +08:00
|
|
|
List of objects published this day
|
2005-07-25 06:21:09 +08:00
|
|
|
``day``
|
2005-07-26 01:18:39 +08:00
|
|
|
The given day (a datetime.datetime object)
|
2005-07-25 06:21:09 +08:00
|
|
|
``previous_day``
|
2005-07-26 01:18:39 +08:00
|
|
|
The previous day (a datetime.datetime object)
|
2005-07-25 06:21:09 +08:00
|
|
|
``next_day``
|
2005-07-26 01:18:39 +08:00
|
|
|
The next day (a datetime.datetime object), or None if the given
|
|
|
|
day is today
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
``archive_today``
|
2005-07-26 01:18:39 +08:00
|
|
|
List of objects for today. Exactly the same as ``archive_day``, except
|
|
|
|
the year/month/day arguments are not given, and today's date is used
|
|
|
|
instead.
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
``object_detail``
|
2005-07-26 01:18:39 +08:00
|
|
|
Individual object page. Requires ``year``/``month``/``day`` arguments like
|
|
|
|
``archive_day``. This function can be used with two types of URLs: either
|
2005-07-25 06:21:09 +08:00
|
|
|
``/year/month/day/slug/`` or ``/year/month/day/object_id/``.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
If you're using the slug-style URLs, you'll need to have a ``slug`` item in
|
2005-07-26 01:18:39 +08:00
|
|
|
your URLconf, and you'll need to pass a ``slug_field`` key in your info
|
|
|
|
dictionary to indicate the name of the slug field.
|
|
|
|
|
|
|
|
If your using the object_id-style URLs, you'll just need to give the URL
|
|
|
|
pattern an ``object_id`` field.
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
You can also pass the ``template_name_field`` argument to indicate that the
|
|
|
|
the object stores the name of its template in a field on the object itself.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-26 11:48:41 +08:00
|
|
|
As in ``archive_day``, ``object_detail`` takes optional ``month_format``
|
|
|
|
and ``day_format`` parameters.
|
|
|
|
|
|
|
|
.. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Using list/detail generic views
|
|
|
|
===============================
|
|
|
|
|
2005-07-26 01:18:39 +08:00
|
|
|
The list-detail generic views (in the ``django.views.generic.list_detail``
|
|
|
|
module) are similar to the data-based ones, except the list-detail views simply
|
|
|
|
have two views: a list of objects, and an individual object page.
|
2005-07-25 06:21:09 +08:00
|
|
|
|
2005-07-26 01:18:39 +08:00
|
|
|
All these views take the same three optional arguments as the date-based ones
|
2005-07-25 06:21:09 +08:00
|
|
|
(and they obviously do not accept or require the date field argument).
|
|
|
|
|
|
|
|
Individual views are:
|
|
|
|
|
|
|
|
``object_list``
|
|
|
|
List of objects.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Takes the following optional arguments:
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
======================= =================================================
|
|
|
|
Argument Description
|
|
|
|
======================= =================================================
|
|
|
|
``paginate_by`` If set to an integer, the view will paginate
|
|
|
|
objects with ``paginate_by`` objects per page.
|
|
|
|
The view will expect a ``page`` GET param with
|
|
|
|
the (zero-indexed) page number.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
|
|
|
``allow_empty`` If ``False`` and there are no objects to display,
|
2005-07-25 06:21:09 +08:00
|
|
|
the view will raise a 404 instead of displaying
|
2005-07-26 01:18:39 +08:00
|
|
|
an empty index page. ``False`` is default.
|
2005-07-25 06:21:09 +08:00
|
|
|
======================= =================================================
|
|
|
|
|
|
|
|
Uses the template ``app_label/module_name__list`` by default.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Has the following template context:
|
|
|
|
|
|
|
|
``object_list``
|
2005-07-26 01:18:39 +08:00
|
|
|
List of objects
|
2005-07-25 06:21:09 +08:00
|
|
|
``is_paginated``
|
2005-07-26 01:18:39 +08:00
|
|
|
Are the results paginated? Either True or False
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
If the results are paginated, the context will have some extra variables:
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
``results_per_page``
|
2005-07-26 01:18:39 +08:00
|
|
|
Number of objects per page
|
2005-07-25 06:21:09 +08:00
|
|
|
``has_next``
|
2005-07-26 01:18:39 +08:00
|
|
|
Is there a next page?
|
2005-07-25 06:21:09 +08:00
|
|
|
``has_previous``
|
2005-07-26 01:18:39 +08:00
|
|
|
Is there a previous page?
|
2005-07-25 06:21:09 +08:00
|
|
|
``page``
|
2005-07-26 01:18:39 +08:00
|
|
|
The current page number
|
2005-07-25 06:21:09 +08:00
|
|
|
``next``
|
2005-07-26 01:18:39 +08:00
|
|
|
The next page number
|
2005-07-25 06:21:09 +08:00
|
|
|
``previous``
|
2005-07-26 01:18:39 +08:00
|
|
|
The previous page
|
2005-07-25 06:21:09 +08:00
|
|
|
``pages``
|
2005-07-26 01:18:39 +08:00
|
|
|
Number of pages total
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
``object_detail``
|
2005-07-26 01:18:39 +08:00
|
|
|
Object detail page. This works like and takes the same arguments as
|
|
|
|
the date-based ``object_detail`` above, except this one, obviously,
|
2005-07-25 06:21:09 +08:00
|
|
|
does not take the year/month/day arguments.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Using create/update/delete generic views
|
|
|
|
========================================
|
|
|
|
|
|
|
|
The ``django.views.generic.create_update`` module contains a set of functions
|
2005-07-26 01:18:39 +08:00
|
|
|
for creating, editing and deleting objects. These views take the same global
|
|
|
|
arguments as the above sets of generic views. They also have a
|
2005-07-25 06:21:09 +08:00
|
|
|
``login_required`` argument which, if ``True``, requires the user to be logged
|
2005-07-26 01:18:39 +08:00
|
|
|
in to have access to the page. (``login_required`` defaults to ``False``.)
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
The create/update/delete views are:
|
|
|
|
|
|
|
|
``create_object``
|
2005-07-26 01:18:39 +08:00
|
|
|
Create a new object. Has an extra optional argument, ``post_save_redirect``,
|
|
|
|
which is a URL to which the view will redirect after saving the object.
|
|
|
|
It defaults to ``object.get_absolute_url()``.
|
|
|
|
|
|
|
|
``post_save_redirect`` may contain dictionary string formatting, which will
|
|
|
|
be interpolated against the object's field attributes. For example, you
|
|
|
|
could use ``post_save_redirect="/polls/%(slug)s/"``.
|
|
|
|
|
|
|
|
Uses the template ``app_label/module_name__form`` by default. This is the
|
|
|
|
same template as the ``update_object`` view below. Your template can tell
|
|
|
|
the different by the presence or absence of ``{{ object }}`` in the
|
|
|
|
context.
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Has the following template context:
|
|
|
|
|
|
|
|
form
|
2005-07-26 01:18:39 +08:00
|
|
|
The form wrapper for the object
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
.. admonition:: Note
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
See the `manipulator and formfield documentation`_ for more information
|
|
|
|
about using form wrappers in templates.
|
|
|
|
|
|
|
|
.. _`manipulator and formfield documentation`: http://www.djangoproject.com/documentation/forms/
|
|
|
|
|
|
|
|
``update_object``
|
2005-07-26 01:18:39 +08:00
|
|
|
Edit an existing object. Has the same extra slug/ID parameters as
|
|
|
|
``list_detail.object_detail`` does (see above), and the same
|
|
|
|
``post_save_redirect`` as ``create_object`` does.
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
Uses the template ``app_label/module_name__form`` by default.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Has the following template context:
|
|
|
|
|
|
|
|
form
|
2005-07-26 01:18:39 +08:00
|
|
|
The form wrapper for the object
|
2005-07-25 06:21:09 +08:00
|
|
|
object
|
2005-07-26 01:18:39 +08:00
|
|
|
The original object being edited
|
2005-07-25 06:21:09 +08:00
|
|
|
|
|
|
|
``delete_object``
|
2005-07-26 01:18:39 +08:00
|
|
|
Delete an existing object. The given object will only actually be deleted
|
|
|
|
if the request method is POST. If this view is fetched with GET, it will
|
|
|
|
display a confirmation page that should contain a form that POSTs to the
|
|
|
|
same URL.
|
|
|
|
|
|
|
|
You must provide the ``post_delete_redirect`` argument to this function, so
|
2005-07-25 06:21:09 +08:00
|
|
|
that the view knows where to go after the object is deleted.
|
2005-07-26 01:18:39 +08:00
|
|
|
|
|
|
|
If fetched with GET, it uses the template
|
|
|
|
``app_label/module_name_s_confirm_delete`` by default. It uses no template
|
|
|
|
if POSTed -- it simply deletes the object and redirects.
|
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
Has the following template context:
|
2005-07-26 01:18:39 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
object
|
2005-07-26 01:18:39 +08:00
|
|
|
The object about to be deleted
|