Small grammar, consistency, and import fixes for the new class-based-views topic guide.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14263 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Gabriel Hurley 2010-10-18 21:08:50 +00:00
parent 26e8e53cf4
commit b05b8cf091
1 changed files with 37 additions and 33 deletions

View File

@ -9,7 +9,7 @@ Class-based generic views
function-based implementation has been deprecated in favor of the function-based implementation has been deprecated in favor of the
class-based approach described here. class-based approach described here.
For the reference to the old on details on the old implementation, For details on the previous generic views implementation,
see the :doc:`topic guide </topics/generic-views>` and see the :doc:`topic guide </topics/generic-views>` and
:doc:`detailed reference </topics/generic-views>`. :doc:`detailed reference </topics/generic-views>`.
@ -38,9 +38,9 @@ Django ships with generic views to do the following:
talk page is an example of what we call a "detail" view. talk page is an example of what we call a "detail" view.
* Present date-based objects in year/month/day archive pages, * Present date-based objects in year/month/day archive pages,
associated detail, and "latest" pages. The Django Weblog's associated detail, and "latest" pages.
(http://www.djangoproject.com/weblog/) year, month, and `The Django Weblog <http://www.djangoproject.com/weblog/>`_'s
day archives are built with these, as would be a typical year, month, and day archives are built with these, as would be a typical
newspaper's archives. newspaper's archives.
* Allow users to create, update, and delete objects -- with or * Allow users to create, update, and delete objects -- with or
@ -53,17 +53,16 @@ tasks developers encounter.
Simple usage Simple usage
============ ============
Class-based generic views (and indeed any class-based views that are Class-based generic views (and any class-based views that inherit from
based on the base classes Django provides) can be configured in two the base classes Django provides) can be configured in two
ways: subclassing, or passing in arguments directly in the URLconf. ways: subclassing, or passing in arguments directly in the URLconf.
When you subclass a class-based view, you can override attributes When you subclass a class-based view, you can override attributes
(such as the template name, ``template_name``) or methods (such as (such as the ``template_name``) or methods (such as ``get_context_data``)
``get_context_data``) in your subclass to provide new values or in your subclass to provide new values or methods. Consider, for example,
methods. Consider, for example, a view that just displays one a view that just displays one template, ``about.html``. Django has a
template, ``about.html``. Django has a generic view to do this - generic view to do this - :class:`~django.views.generic.base.TemplateView` -
:class:`~django.views.generic.base.TemplateView` - so we can just so we can just subclass it, and override the template name::
subclass it, and override the template name::
# some_app/views.py # some_app/views.py
from django.views.generic import TemplateView from django.views.generic import TemplateView
@ -104,7 +103,7 @@ Generic views of objects
:class:`~django.views.generic.base.TemplateView` certainly is useful, :class:`~django.views.generic.base.TemplateView` certainly is useful,
but Django's generic views really shine when it comes to presenting but Django's generic views really shine when it comes to presenting
views on your database content. Because it's such a common task, views of your database content. Because it's such a common task,
Django comes with a handful of built-in generic views that make Django comes with a handful of built-in generic views that make
generating list and detail views of objects incredibly easy. generating list and detail views of objects incredibly easy.
@ -175,7 +174,7 @@ might look like the following::
That's really all there is to it. All the cool features of generic views come That's really all there is to it. All the cool features of generic views come
from changing the "info" dictionary passed to the generic view. The from changing the "info" dictionary passed to the generic view. The
:doc:`generic views reference</ref/class-based-views>` documents all the generic :doc:`generic views reference</ref/class-based-views>` documents all the generic
views and all their options in detail; the rest of this document will consider views and their options in detail; the rest of this document will consider
some of the common ways you might customize and extend generic views. some of the common ways you might customize and extend generic views.
@ -201,10 +200,10 @@ Making "friendly" template contexts
----------------------------------- -----------------------------------
You might have noticed that our sample publisher list template stores all the You might have noticed that our sample publisher list template stores all the
books in a variable named ``object_list``. While this works just fine, it isn't publishers in a variable named ``object_list``. While this works just fine, it
all that "friendly" to template authors: they have to "just know" that they're isn't all that "friendly" to template authors: they have to "just know" that
dealing with publishers here. A better name for that variable would be they're dealing with publishers here. A more obvious name for that variable
``publisher_list``; that variable's content is pretty obvious. would be ``publisher_list``.
We can change the name of that variable easily with the ``context_object_name`` We can change the name of that variable easily with the ``context_object_name``
attribute - here, we'll override it in the URLconf, since it's a simple change: attribute - here, we'll override it in the URLconf, since it's a simple change:
@ -237,11 +236,11 @@ However, there is; you can subclass
implementation of the ``get_context_data`` method. The default implementation of the ``get_context_data`` method. The default
implementation of this that comes with implementation of this that comes with
:class:`~django.views.generic.detail.DetailView` simply adds in the :class:`~django.views.generic.detail.DetailView` simply adds in the
object being displayed to the template, but we can override it to show object being displayed to the template, but you can override it to show
more:: more::
from django.views.generic import DetailView from django.views.generic import DetailView
from some_app.models import Publisher, Book from books.models import Publisher, Book
class PublisherDetailView(DetailView): class PublisherDetailView(DetailView):
@ -268,7 +267,7 @@ specify the objects that the view will operate upon -- you can also
specify the list of objects using the ``queryset`` argument:: specify the list of objects using the ``queryset`` argument::
from django.views.generic import DetailView from django.views.generic import DetailView
from some_app.models import Publisher, Book from books.models import Publisher, Book
class PublisherDetailView(DetailView): class PublisherDetailView(DetailView):
@ -279,8 +278,9 @@ Specifying ``model = Publisher`` is really just shorthand for saying
``queryset = Publisher.objects.all()``. However, by using ``queryset`` ``queryset = Publisher.objects.all()``. However, by using ``queryset``
to define a filtered list of objects you can be more specific about the to define a filtered list of objects you can be more specific about the
objects that will be visible in the view (see :doc:`/topics/db/queries` objects that will be visible in the view (see :doc:`/topics/db/queries`
for more information about ``QuerySet`` objects, and see the for more information about :class:`QuerySet` objects, and see the
:doc:`generic views reference</ref/generic-views>` for the complete details). :doc:`class-based views reference </ref/class-based-views>` for the complete
details).
To pick a simple example, we might want to order a list of books by To pick a simple example, we might want to order a list of books by
publication date, with the most recent first:: publication date, with the most recent first::
@ -304,7 +304,7 @@ technique (here, illustrated using subclassing rather than by passing arguments
in the URLconf):: in the URLconf)::
from django.views.generic import ListView from django.views.generic import ListView
from some_app.models import Book from books.models import Book
class AcmeBookListView(ListView): class AcmeBookListView(ListView):
@ -326,7 +326,7 @@ We'll deal with this problem in the next section.
If you get a 404 when requesting ``/books/acme/``, check to ensure you If you get a 404 when requesting ``/books/acme/``, check to ensure you
actually have a Publisher with the name 'ACME Publishing'. Generic actually have a Publisher with the name 'ACME Publishing'. Generic
views have an ``allow_empty`` parameter for this case. See the views have an ``allow_empty`` parameter for this case. See the
:doc:`generic views reference</ref/class-based-views>` for more details. :doc:`class-based-views reference</ref/class-based-views>` for more details.
Dynamic filtering Dynamic filtering
@ -337,9 +337,10 @@ key in the URL. Earlier we hard-coded the publisher's name in the URLconf, but
what if we wanted to write a view that displayed all the books by some arbitrary what if we wanted to write a view that displayed all the books by some arbitrary
publisher? publisher?
Handily, the ListView has a ``get_queryset`` method we can override. Previously, Handily, the ListView has a
it has just been returning the value of the ``queryset`` attribute, but now we :meth:`~django.views.generic.detail.ListView.get_queryset` method we can
can add more logic. override. Previously, it has just been returning the value of the ``queryset``
attribute, but now we can add more logic.
The key part to making this work is that when class-based views are called, The key part to making this work is that when class-based views are called,
various useful things are stored on ``self``; as well as the request various useful things are stored on ``self``; as well as the request
@ -348,7 +349,7 @@ various useful things are stored on ``self``; as well as the request
Here, we have a URLconf with a single captured group:: Here, we have a URLconf with a single captured group::
from some_app.views import PublisherBookListView from books.views import PublisherBookListView
urlpatterns = patterns('', urlpatterns = patterns('',
(r'^books/(\w+)/$', PublisherBookListView.as_view()), (r'^books/(\w+)/$', PublisherBookListView.as_view()),
@ -358,7 +359,7 @@ Next, we'll write the ``PublisherBookListView`` view itself::
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.views.generic import ListView from django.views.generic import ListView
from some_app.models import Book, Publisher from books.models import Book, Publisher
class PublisherBookListView(ListView): class PublisherBookListView(ListView):
@ -420,7 +421,7 @@ custom view:
.. parsed-literal:: .. parsed-literal::
from some_app.views import AuthorDetailView from books.views import AuthorDetailView
urlpatterns = patterns('', urlpatterns = patterns('',
#... #...
@ -431,7 +432,7 @@ Then we'd write our new view - ``get_object`` is the method that retrieves the
object, so we simply override it and wrap the call:: object, so we simply override it and wrap the call::
import datetime import datetime
from some_app.models import Author from books.models import Author
from django.views.generic import DetailView from django.views.generic import DetailView
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@ -472,12 +473,15 @@ Each generic view is composed out of a series of mixins, and each
mixin contributes a little piece of the entire view. Some of these mixin contributes a little piece of the entire view. Some of these
mixins -- such as mixins -- such as
:class:`~django.views.generic.base.TemplateResponseMixin` -- are :class:`~django.views.generic.base.TemplateResponseMixin` -- are
specifically designed for rendering content to a HTML response using a specifically designed for rendering content to an HTML response using a
template. However, you can write your own mixins that perform template. However, you can write your own mixins that perform
different rendering behavior. different rendering behavior.
For example, a simple JSON mixin might look something like this:: For example, a simple JSON mixin might look something like this::
from django import http
from django.utils import simplejson as json
class JSONResponseMixin(object): class JSONResponseMixin(object):
def render_to_response(self, context): def render_to_response(self, context):
"Returns a JSON response containing 'context' as payload" "Returns a JSON response containing 'context' as payload"