Merge pull request #261 from issackelly/cbv-full-list

Create headings and expand CBV docs so that the "Built-In CBV" docs include a full list.
This commit is contained in:
Tim Graham 2012-08-17 07:57:39 -07:00
commit 4eaf73d7bc
10 changed files with 121 additions and 27 deletions

View File

@ -8,6 +8,9 @@ themselves or inherited from. They may not provide all the capabilities
required for projects, in which case there are Mixins and Generic class-based required for projects, in which case there are Mixins and Generic class-based
views. views.
View
----
.. class:: django.views.generic.base.View .. class:: django.views.generic.base.View
The master class-based base view. All other class-based views inherit from The master class-based base view. All other class-based views inherit from
@ -31,13 +34,13 @@ views.
**Example urls.py**:: **Example urls.py**::
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
from myapp.views import MyView from myapp.views import MyView
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^mine/$', MyView.as_view(), name='my-view'), url(r'^mine/$', MyView.as_view(), name='my-view'),
) )
**Methods** **Methods**
.. method:: dispatch(request, *args, **kwargs) .. method:: dispatch(request, *args, **kwargs)
@ -61,13 +64,16 @@ views.
The default implementation returns ``HttpResponseNotAllowed`` with list The default implementation returns ``HttpResponseNotAllowed`` with list
of allowed methods in plain text. of allowed methods in plain text.
.. note:: .. note::
Documentation on class-based views is a work in progress. As yet, only the Documentation on class-based views is a work in progress. As yet, only the
methods defined directly on the class are documented here, not methods methods defined directly on the class are documented here, not methods
defined on superclasses. defined on superclasses.
TemplateView
------------
.. class:: django.views.generic.base.TemplateView .. class:: django.views.generic.base.TemplateView
Renders a given template, passing it a ``{{ params }}`` template variable, Renders a given template, passing it a ``{{ params }}`` template variable,
@ -84,22 +90,22 @@ views.
1. :meth:`dispatch()` 1. :meth:`dispatch()`
2. :meth:`http_method_not_allowed()` 2. :meth:`http_method_not_allowed()`
3. :meth:`get_context_data()` 3. :meth:`get_context_data()`
**Example views.py**:: **Example views.py**::
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from articles.models import Article from articles.models import Article
class HomePageView(TemplateView): class HomePageView(TemplateView):
template_name = "home.html" template_name = "home.html"
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(HomePageView, self).get_context_data(**kwargs) context = super(HomePageView, self).get_context_data(**kwargs)
context['latest_articles'] = Article.objects.all()[:5] context['latest_articles'] = Article.objects.all()[:5]
return context return context
**Example urls.py**:: **Example urls.py**::
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
@ -126,12 +132,15 @@ views.
* ``params``: The dictionary of keyword arguments captured from the URL * ``params``: The dictionary of keyword arguments captured from the URL
pattern that served the view. pattern that served the view.
.. note:: .. note::
Documentation on class-based views is a work in progress. As yet, only the Documentation on class-based views is a work in progress. As yet, only the
methods defined directly on the class are documented here, not methods methods defined directly on the class are documented here, not methods
defined on superclasses. defined on superclasses.
RedirectView
------------
.. class:: django.views.generic.base.RedirectView .. class:: django.views.generic.base.RedirectView
Redirects to a given URL. Redirects to a given URL.
@ -159,7 +168,7 @@ views.
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.views.generic.base import RedirectView from django.views.generic.base import RedirectView
from articles.models import Article from articles.models import Article
class ArticleCounterRedirectView(RedirectView): class ArticleCounterRedirectView(RedirectView):
@ -176,7 +185,7 @@ views.
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
from django.views.generic.base import RedirectView from django.views.generic.base import RedirectView
from article.views import ArticleCounterRedirectView from article.views import ArticleCounterRedirectView
urlpatterns = patterns('', urlpatterns = patterns('',
@ -217,7 +226,7 @@ views.
behavior they wish, as long as the method returns a redirect-ready URL behavior they wish, as long as the method returns a redirect-ready URL
string. string.
.. note:: .. note::
Documentation on class-based views is a work in progress. As yet, only the Documentation on class-based views is a work in progress. As yet, only the
methods defined directly on the class are documented here, not methods methods defined directly on the class are documented here, not methods

View File

@ -5,6 +5,9 @@ Generic date views
Date-based generic views (in the module :mod:`django.views.generic.dates`) Date-based generic views (in the module :mod:`django.views.generic.dates`)
are views for displaying drilldown pages for date-based data. are views for displaying drilldown pages for date-based data.
ArchiveIndexView
----------------
.. class:: django.views.generic.dates.ArchiveIndexView .. class:: django.views.generic.dates.ArchiveIndexView
A top-level index page showing the "latest" objects, by date. Objects with A top-level index page showing the "latest" objects, by date. Objects with
@ -21,12 +24,15 @@ are views for displaying drilldown pages for date-based data.
* :class:`django.views.generic.list.MultipleObjectMixin` * :class:`django.views.generic.list.MultipleObjectMixin`
* :class:`django.views.generic.dates.DateMixin` * :class:`django.views.generic.dates.DateMixin`
* :class:`django.views.generic.base.View` * :class:`django.views.generic.base.View`
**Notes** **Notes**
* Uses a default ``context_object_name`` of ``latest``. * Uses a default ``context_object_name`` of ``latest``.
* Uses a default ``template_name_suffix`` of ``_archive``. * Uses a default ``template_name_suffix`` of ``_archive``.
YearArchiveView
---------------
.. class:: django.views.generic.dates.YearArchiveView .. class:: django.views.generic.dates.YearArchiveView
A yearly archive page showing all available months in a given year. Objects A yearly archive page showing all available months in a given year. Objects
@ -86,6 +92,9 @@ are views for displaying drilldown pages for date-based data.
* Uses a default ``template_name_suffix`` of ``_archive_year``. * Uses a default ``template_name_suffix`` of ``_archive_year``.
MonthArchiveView
----------------
.. class:: django.views.generic.dates.MonthArchiveView .. class:: django.views.generic.dates.MonthArchiveView
A monthly archive page showing all objects in a given month. Objects with a A monthly archive page showing all objects in a given month. Objects with a
@ -134,6 +143,9 @@ are views for displaying drilldown pages for date-based data.
* Uses a default ``template_name_suffix`` of ``_archive_month``. * Uses a default ``template_name_suffix`` of ``_archive_month``.
WeekArchiveView
---------------
.. class:: django.views.generic.dates.WeekArchiveView .. class:: django.views.generic.dates.WeekArchiveView
A weekly archive page showing all objects in a given week. Objects with a A weekly archive page showing all objects in a given week. Objects with a
@ -175,6 +187,9 @@ are views for displaying drilldown pages for date-based data.
* Uses a default ``template_name_suffix`` of ``_archive_week``. * Uses a default ``template_name_suffix`` of ``_archive_week``.
DayArchiveView
--------------
.. class:: django.views.generic.dates.DayArchiveView .. class:: django.views.generic.dates.DayArchiveView
A day archive page showing all objects in a given day. Days in the future A day archive page showing all objects in a given day. Days in the future
@ -225,6 +240,9 @@ are views for displaying drilldown pages for date-based data.
* Uses a default ``template_name_suffix`` of ``_archive_day``. * Uses a default ``template_name_suffix`` of ``_archive_day``.
TodayArchiveView
----------------
.. class:: django.views.generic.dates.TodayArchiveView .. class:: django.views.generic.dates.TodayArchiveView
A day archive page showing all objects for *today*. This is exactly the A day archive page showing all objects for *today*. This is exactly the
@ -246,6 +264,10 @@ are views for displaying drilldown pages for date-based data.
* :class:`django.views.generic.dates.DateMixin` * :class:`django.views.generic.dates.DateMixin`
* :class:`django.views.generic.base.View` * :class:`django.views.generic.base.View`
DateDetailView
--------------
.. class:: django.views.generic.dates.DateDetailView .. class:: django.views.generic.dates.DateDetailView
A page representing an individual object. If the object has a date value in A page representing an individual object. If the object has a date value in

View File

@ -5,6 +5,9 @@ Generic display views
The two following generic class-based views are designed to display data. On The two following generic class-based views are designed to display data. On
many projects they are typically the most commonly used views. many projects they are typically the most commonly used views.
DetailView
----------
.. class:: django.views.generic.detail.DetailView .. class:: django.views.generic.detail.DetailView
While this view is executing, ``self.object`` will contain the object that While this view is executing, ``self.object`` will contain the object that
@ -39,7 +42,7 @@ many projects they are typically the most commonly used views.
from articles.models import Article from articles.models import Article
class ArticleDetailView(DetailView): class ArticleDetailView(DetailView):
model = Article model = Article
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
@ -55,7 +58,10 @@ many projects they are typically the most commonly used views.
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^(?P<slug>[-_\w]+)/$', ArticleDetailView.as_view(), name='article-detail'), url(r'^(?P<slug>[-_\w]+)/$', ArticleDetailView.as_view(), name='article-detail'),
) )
ListView
--------
.. class:: django.views.generic.list.ListView .. class:: django.views.generic.list.ListView

View File

@ -2,7 +2,7 @@
Generic editing views Generic editing views
===================== =====================
The following views are described on this page and provide a foundation for The following views are described on this page and provide a foundation for
editing content: editing content:
* :class:`django.views.generic.edit.FormView` * :class:`django.views.generic.edit.FormView`
@ -13,7 +13,7 @@ editing content:
.. note:: .. note::
Some of the examples on this page assume that a model titled 'Author' Some of the examples on this page assume that a model titled 'Author'
has been defined. For these cases we assume the following has been defined has been defined. For these cases we assume the following has been defined
in `myapp/models.py`:: in `myapp/models.py`::
from django import models from django import models
@ -25,6 +25,9 @@ editing content:
def get_absolute_url(self): def get_absolute_url(self):
return reverse('author-detail', kwargs={'pk': self.pk}) return reverse('author-detail', kwargs={'pk': self.pk})
FormView
--------
.. class:: django.views.generic.edit.FormView .. class:: django.views.generic.edit.FormView
A view that displays a form. On error, redisplays the form with validation A view that displays a form. On error, redisplays the form with validation
@ -69,6 +72,8 @@ editing content:
form.send_email() form.send_email()
return super(ContactView, self).form_valid(form) return super(ContactView, self).form_valid(form)
CreateView
----------
.. class:: django.views.generic.edit.CreateView .. class:: django.views.generic.edit.CreateView
@ -94,7 +99,7 @@ editing content:
.. attribute:: template_name_suffix .. attribute:: template_name_suffix
The CreateView page displayed to a GET request uses a The CreateView page displayed to a GET request uses a
``template_name_suffix`` of ``'_form.html'``. For ``template_name_suffix`` of ``'_form.html'``. For
example, changing this attribute to ``'_create_form.html'`` for a view example, changing this attribute to ``'_create_form.html'`` for a view
creating objects for the the example `Author` model would cause the the creating objects for the the example `Author` model would cause the the
default `template_name` to be ``'myapp/author_create_form.html'``. default `template_name` to be ``'myapp/author_create_form.html'``.
@ -107,6 +112,9 @@ editing content:
class AuthorCreate(CreateView): class AuthorCreate(CreateView):
model = Author model = Author
UpdateView
----------
.. class:: django.views.generic.edit.UpdateView .. class:: django.views.generic.edit.UpdateView
A view that displays a form for editing an existing object, redisplaying A view that displays a form for editing an existing object, redisplaying
@ -133,10 +141,10 @@ editing content:
.. attribute:: template_name_suffix .. attribute:: template_name_suffix
The UpdateView page displayed to a GET request uses a The UpdateView page displayed to a GET request uses a
``template_name_suffix`` of ``'_form.html'``. For ``template_name_suffix`` of ``'_form.html'``. For
example, changing this attribute to ``'_update_form.html'`` for a view example, changing this attribute to ``'_update_form.html'`` for a view
updating objects for the the example `Author` model would cause the the updating objects for the the example `Author` model would cause the the
default `template_name` to be ``'myapp/author_update_form.html'``. default `template_name` to be ``'myapp/author_update_form.html'``.
**Example views.py**:: **Example views.py**::
@ -146,6 +154,9 @@ editing content:
class AuthorUpdate(UpdateView): class AuthorUpdate(UpdateView):
model = Author model = Author
DeleteView
----------
.. class:: django.views.generic.edit.DeleteView .. class:: django.views.generic.edit.DeleteView
A view that displays a confirmation page and deletes an existing object. A view that displays a confirmation page and deletes an existing object.
@ -171,10 +182,10 @@ editing content:
.. attribute:: template_name_suffix .. attribute:: template_name_suffix
The DeleteView page displayed to a GET request uses a The DeleteView page displayed to a GET request uses a
``template_name_suffix`` of ``'_confirm_delete.html'``. For ``template_name_suffix`` of ``'_confirm_delete.html'``. For
example, changing this attribute to ``'_check_delete.html'`` for a view example, changing this attribute to ``'_check_delete.html'`` for a view
deleting objects for the the example `Author` model would cause the the deleting objects for the the example `Author` model would cause the the
default `template_name` to be ``'myapp/author_check_delete.html'``. default `template_name` to be ``'myapp/author_check_delete.html'``.
**Example views.py**:: **Example views.py**::
@ -185,4 +196,4 @@ editing content:
class AuthorDelete(DeleteView): class AuthorDelete(DeleteView):
model = Author model = Author
success_url = reverse_lazy('author-list') success_url = reverse_lazy('author-list')

View File

@ -6,7 +6,7 @@ Class-based views API reference. For introductory material, see
:doc:`/topics/class-based-views/index`. :doc:`/topics/class-based-views/index`.
.. toctree:: .. toctree::
:maxdepth: 1 :maxdepth: 3
base base
generic-display generic-display

View File

@ -3,6 +3,9 @@ Date-based mixins
================= =================
YearMixin
---------
.. class:: django.views.generic.dates.YearMixin .. class:: django.views.generic.dates.YearMixin
A mixin that can be used to retrieve and provide parsing information for a A mixin that can be used to retrieve and provide parsing information for a
@ -36,6 +39,9 @@ Date-based mixins
Raises a 404 if no valid year specification can be found. Raises a 404 if no valid year specification can be found.
MonthMixin
----------
.. class:: django.views.generic.dates.MonthMixin .. class:: django.views.generic.dates.MonthMixin
A mixin that can be used to retrieve and provide parsing information for a A mixin that can be used to retrieve and provide parsing information for a
@ -82,6 +88,9 @@ Date-based mixins
date provided. If ``allow_empty = False``, returns the previous month date provided. If ``allow_empty = False``, returns the previous month
that contained data. that contained data.
DayMixin
--------
.. class:: django.views.generic.dates.DayMixin .. class:: django.views.generic.dates.DayMixin
A mixin that can be used to retrieve and provide parsing information for a A mixin that can be used to retrieve and provide parsing information for a
@ -127,6 +136,9 @@ Date-based mixins
Returns a date object containing the previous day. If Returns a date object containing the previous day. If
``allow_empty = False``, returns the previous day that contained data. ``allow_empty = False``, returns the previous day that contained data.
WeekMixin
---------
.. class:: django.views.generic.dates.WeekMixin .. class:: django.views.generic.dates.WeekMixin
A mixin that can be used to retrieve and provide parsing information for a A mixin that can be used to retrieve and provide parsing information for a
@ -161,6 +173,9 @@ Date-based mixins
Raises a 404 if no valid week specification can be found. Raises a 404 if no valid week specification can be found.
DateMixin
---------
.. class:: django.views.generic.dates.DateMixin .. class:: django.views.generic.dates.DateMixin
A mixin class providing common behavior for all date-based views. A mixin class providing common behavior for all date-based views.
@ -204,6 +219,9 @@ Date-based mixins
is greater than the current date/time. Returns is greater than the current date/time. Returns
:attr:`DateMixin.allow_future` by default. :attr:`DateMixin.allow_future` by default.
BaseDateListView
----------------
.. class:: django.views.generic.dates.BaseDateListView .. class:: django.views.generic.dates.BaseDateListView
A base class that provides common behavior for all date-based views. There A base class that provides common behavior for all date-based views. There

View File

@ -14,6 +14,9 @@ The following mixins are used to construct Django's editing views:
Examples of how these are combined into editing views can be found at Examples of how these are combined into editing views can be found at
the documentation on ``Generic editing views``. the documentation on ``Generic editing views``.
FormMixin
---------
.. class:: django.views.generic.edit.FormMixin .. class:: django.views.generic.edit.FormMixin
A mixin class that provides facilities for creating and displaying forms. A mixin class that provides facilities for creating and displaying forms.
@ -90,6 +93,9 @@ The following mixins are used to construct Django's editing views:
:meth:`~django.views.generic.FormMixin.form_invalid`. :meth:`~django.views.generic.FormMixin.form_invalid`.
ModelFormMixin
--------------
.. class:: django.views.generic.edit.ModelFormMixin .. class:: django.views.generic.edit.ModelFormMixin
A form mixin that works on ModelForms, rather than a standalone form. A form mixin that works on ModelForms, rather than a standalone form.
@ -147,12 +153,16 @@ The following mixins are used to construct Django's editing views:
.. method:: form_invalid() .. method:: form_invalid()
Renders a response, providing the invalid form as context. Renders a response, providing the invalid form as context.
ProcessFormView
---------------
.. class:: django.views.generic.edit.ProcessFormView .. class:: django.views.generic.edit.ProcessFormView
A mixin that provides basic HTTP GET and POST workflow. A mixin that provides basic HTTP GET and POST workflow.
.. note:: .. note::
This is named 'ProcessFormView' and inherits directly from This is named 'ProcessFormView' and inherits directly from
:class:`django.views.generic.base.View`, but breaks if used :class:`django.views.generic.base.View`, but breaks if used

View File

@ -2,6 +2,9 @@
Multiple object mixins Multiple object mixins
====================== ======================
MultipleObjectMixin
-------------------
.. class:: django.views.generic.list.MultipleObjectMixin .. class:: django.views.generic.list.MultipleObjectMixin
A mixin that can be used to display a list of objects. A mixin that can be used to display a list of objects.
@ -148,6 +151,9 @@ Multiple object mixins
this context variable will be ``None``. this context variable will be ``None``.
MultipleObjectTemplateResponseMixin
-----------------------------------
.. class:: django.views.generic.list.MultipleObjectTemplateResponseMixin .. class:: django.views.generic.list.MultipleObjectTemplateResponseMixin
A mixin class that performs template-based response rendering for views A mixin class that performs template-based response rendering for views

View File

@ -2,6 +2,9 @@
Simple mixins Simple mixins
============= =============
ContextMixin
------------
.. class:: django.views.generic.base.ContextMixin .. class:: django.views.generic.base.ContextMixin
.. versionadded:: 1.5 .. versionadded:: 1.5
@ -17,6 +20,9 @@ Simple mixins
Returns a dictionary representing the template context. The Returns a dictionary representing the template context. The
keyword arguments provided will make up the returned context. keyword arguments provided will make up the returned context.
TemplateResponseMixin
---------------------
.. class:: django.views.generic.base.TemplateResponseMixin .. class:: django.views.generic.base.TemplateResponseMixin
Provides a mechanism to construct a Provides a mechanism to construct a

View File

@ -2,6 +2,9 @@
Single object mixins Single object mixins
==================== ====================
SingleObjectMixin
-----------------
.. class:: django.views.generic.detail.SingleObjectMixin .. class:: django.views.generic.detail.SingleObjectMixin
Provides a mechanism for looking up an object associated with the Provides a mechanism for looking up an object associated with the
@ -86,6 +89,9 @@ Single object mixins
``context_object_name`` is specified, that variable will also be ``context_object_name`` is specified, that variable will also be
set in the context, with the same value as ``object``. set in the context, with the same value as ``object``.
SingleObjectTemplateResponseMixin
---------------------------------
.. class:: django.views.generic.detail.SingleObjectTemplateResponseMixin .. class:: django.views.generic.detail.SingleObjectTemplateResponseMixin
A mixin class that performs template-based response rendering for views A mixin class that performs template-based response rendering for views