mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #6735 -- Added class-based views.
This patch is the result of the work of many people, over many years. To try and thank individuals would inevitably lead to many people being left out or forgotten -- so rather than try to give a list that will inevitably be incomplete, I'd like to thank *everybody* who contributed in any way, big or small, with coding, testing, feedback and/or documentation over the multi-year process of getting this into trunk. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14254 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
535
docs/topics/class-based-views.txt
Normal file
535
docs/topics/class-based-views.txt
Normal file
@@ -0,0 +1,535 @@
|
||||
=========================
|
||||
Class-based generic views
|
||||
=========================
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
.. note::
|
||||
Prior to Django 1.3, generic views were implemented as functions. The
|
||||
function-based implementation has been deprecated in favor of the
|
||||
class-based approach described here.
|
||||
|
||||
For the reference to the old on details on the old implementation,
|
||||
see the :doc:`topic guide </topics/generic-views>` and
|
||||
:doc:`detailed reference </topics/generic-views>`.
|
||||
|
||||
Writing Web applications can be monotonous, because we repeat certain patterns
|
||||
again and again. Django tries to take away some of that monotony at the model
|
||||
and template layers, but Web developers also experience this boredom at the view
|
||||
level.
|
||||
|
||||
Django's *generic views* were developed to ease that pain. They take certain
|
||||
common idioms and patterns found in view development and abstract them so that
|
||||
you can quickly write common views of data without having to write too much
|
||||
code.
|
||||
|
||||
We can recognize certain common tasks, like displaying a list of objects, and
|
||||
write code that displays a list of *any* object. Then the model in question can
|
||||
be passed as an extra argument to the URLconf.
|
||||
|
||||
Django ships with generic views to do the following:
|
||||
|
||||
* Perform common "simple" tasks: redirect to a different page and
|
||||
render a given template.
|
||||
|
||||
* Display list and detail pages for a single object. If we were creating an
|
||||
application to manage conferences then a ``TalkListView`` and a
|
||||
``RegisteredUserListView`` would be examples of list views. A single
|
||||
talk page is an example of what we call a "detail" view.
|
||||
|
||||
* Present date-based objects in year/month/day archive pages,
|
||||
associated detail, and "latest" pages. The Django Weblog's
|
||||
(http://www.djangoproject.com/weblog/) year, month, and
|
||||
day archives are built with these, as would be a typical
|
||||
newspaper's archives.
|
||||
|
||||
* Allow users to create, update, and delete objects -- with or
|
||||
without authorization.
|
||||
|
||||
Taken together, these views provide easy interfaces to perform the most common
|
||||
tasks developers encounter.
|
||||
|
||||
|
||||
Simple usage
|
||||
============
|
||||
|
||||
Class-based generic views (and indeed any class-based views that are
|
||||
based on the base classes Django provides) can be configured in two
|
||||
ways: subclassing, or passing in arguments directly in the URLconf.
|
||||
|
||||
When you subclass a class-based view, you can override attributes
|
||||
(such as the template name, ``template_name``) or methods (such as
|
||||
``get_context_data``) in your subclass to provide new values or
|
||||
methods. Consider, for example, a view that just displays one
|
||||
template, ``about.html``. Django has a generic view to do this -
|
||||
:class:`~django.views.generic.base.TemplateView` - so we can just
|
||||
subclass it, and override the template name::
|
||||
|
||||
# some_app/views.py
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
class AboutView(TemplateView):
|
||||
template_name = "about.html"
|
||||
|
||||
Then, we just need to add this new view into our URLconf. As the class-based
|
||||
views themselves are classes, we point the URL to the as_view class method
|
||||
instead, which is the entrypoint for class-based views::
|
||||
|
||||
# urls.py
|
||||
from django.conf.urls.defaults import *
|
||||
from some_app.views import AboutView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^about/', AboutView.as_view()),
|
||||
)
|
||||
|
||||
Alternatively, if you're only changing a few simple attributes on a
|
||||
class-based view, you can simply pass the new attributes into the as_view
|
||||
method call itself::
|
||||
|
||||
from django.conf.urls.defaults import *
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^about/', TemplateView.as_view(template_name="about.html")),
|
||||
)
|
||||
|
||||
A similar overriding pattern can be used for the ``url`` attribute on
|
||||
:class:`~django.views.generic.base.RedirectView`, another simple
|
||||
generic view.
|
||||
|
||||
|
||||
Generic views of objects
|
||||
========================
|
||||
|
||||
:class:`~django.views.generic.base.TemplateView` certainly is useful,
|
||||
but Django's generic views really shine when it comes to presenting
|
||||
views on your database content. Because it's such a common task,
|
||||
Django comes with a handful of built-in generic views that make
|
||||
generating list and detail views of objects incredibly easy.
|
||||
|
||||
Let's take a look at one of these generic views: the "object list" view. We'll
|
||||
be using these models::
|
||||
|
||||
# models.py
|
||||
from django.db import models
|
||||
|
||||
class Publisher(models.Model):
|
||||
name = models.CharField(max_length=30)
|
||||
address = models.CharField(max_length=50)
|
||||
city = models.CharField(max_length=60)
|
||||
state_province = models.CharField(max_length=30)
|
||||
country = models.CharField(max_length=50)
|
||||
website = models.URLField()
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
ordering = ["-name"]
|
||||
|
||||
class Book(models.Model):
|
||||
title = models.CharField(max_length=100)
|
||||
authors = models.ManyToManyField('Author')
|
||||
publisher = models.ForeignKey(Publisher)
|
||||
publication_date = models.DateField()
|
||||
|
||||
To build a list page of all publishers, we'd use a URLconf along these lines::
|
||||
|
||||
from django.conf.urls.defaults import *
|
||||
from django.views.generic import ListView
|
||||
from books.models import Publisher
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^publishers/$', ListView.as_view(
|
||||
model=Publisher,
|
||||
)),
|
||||
)
|
||||
|
||||
That's all the Python code we need to write. We still need to write a template,
|
||||
however. We could explicitly tell the view which template to use
|
||||
by including a ``template_name`` key in the arguments to as_view, but in
|
||||
the absence of an explicit template Django will infer one from the object's
|
||||
name. In this case, the inferred template will be
|
||||
``"books/publisher_list.html"`` -- the "books" part comes from the name of the
|
||||
app that defines the model, while the "publisher" bit is just the lowercased
|
||||
version of the model's name.
|
||||
|
||||
.. highlightlang:: html+django
|
||||
|
||||
This template will be rendered against a context containing a variable called
|
||||
``object_list`` that contains all the publisher objects. A very simple template
|
||||
might look like the following::
|
||||
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Publishers</h2>
|
||||
<ul>
|
||||
{% for publisher in object_list %}
|
||||
<li>{{ publisher.name }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
||||
|
||||
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
|
||||
: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
|
||||
some of the common ways you might customize and extend generic views.
|
||||
|
||||
|
||||
Extending generic views
|
||||
=======================
|
||||
|
||||
.. highlightlang:: python
|
||||
|
||||
There's no question that using generic views can speed up development
|
||||
substantially. In most projects, however, there comes a moment when the
|
||||
generic views no longer suffice. Indeed, the most common question asked by new
|
||||
Django developers is how to make generic views handle a wider array of
|
||||
situations.
|
||||
|
||||
This is one of the reasons generic views were redesigned for the 1.3 release -
|
||||
previously, they were just view functions with a bewildering array of options;
|
||||
now, rather than passing in a large amount of configuration in the URLconf,
|
||||
the recommended way to extend generic views is to subclass them, and override
|
||||
their attributes or methods.
|
||||
|
||||
|
||||
Making "friendly" template contexts
|
||||
-----------------------------------
|
||||
|
||||
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
|
||||
all that "friendly" to template authors: they have to "just know" that they're
|
||||
dealing with publishers here. A better name for that variable would be
|
||||
``publisher_list``; that variable's content is pretty obvious.
|
||||
|
||||
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:
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^publishers/$', ListView.as_view(
|
||||
model=Publisher,
|
||||
**context_object_name = "publisher_list",**
|
||||
)),
|
||||
)
|
||||
|
||||
Providing a useful ``context_object_name`` is always a good idea. Your
|
||||
coworkers who design templates will thank you.
|
||||
|
||||
|
||||
Adding extra context
|
||||
--------------------
|
||||
|
||||
Often you simply need to present some extra information beyond that
|
||||
provided by the generic view. For example, think of showing a list of
|
||||
all the books on each publisher detail page. The
|
||||
:class:`~django.views.generic.detail.DetailView` generic view provides
|
||||
the publisher to the context, but it seems there's no way to get
|
||||
additional information in that template.
|
||||
|
||||
However, there is; you can subclass
|
||||
:class:`~django.views.generic.detail.DetailView` and provide your own
|
||||
implementation of the ``get_context_data`` method. The default
|
||||
implementation of this that comes with
|
||||
:class:`~django.views.generic.detail.DetailView` simply adds in the
|
||||
object being displayed to the template, but we can override it to show
|
||||
more::
|
||||
|
||||
from django.views.generic import DetailView
|
||||
from some_app.models import Publisher, Book
|
||||
|
||||
class PublisherDetailView(DetailView):
|
||||
|
||||
context_object_name = "publisher"
|
||||
model = Publisher
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
# Call the base implementation first to get a context
|
||||
context = DetailView.get_context_data(self, **kwargs)
|
||||
# Add in a QuerySet of all the books
|
||||
context['book_list'] = Book.objects.all()
|
||||
return context
|
||||
|
||||
|
||||
Viewing subsets of objects
|
||||
--------------------------
|
||||
|
||||
Now let's take a closer look at the ``model`` argument we've been
|
||||
using all along. The ``model`` argument, which specifies the database
|
||||
model that the view will operate upon, is available on all the
|
||||
generic views that operate on a single object or a collection of
|
||||
objects. However, the ``model`` argument is not the only way to
|
||||
specify the objects that the view will operate upon -- you can also
|
||||
specify the list of objects using the ``queryset`` argument::
|
||||
|
||||
from django.views.generic import DetailView
|
||||
from some_app.models import Publisher, Book
|
||||
|
||||
class PublisherDetailView(DetailView):
|
||||
|
||||
context_object_name = "publisher"
|
||||
queryset = Publisher.object.all()
|
||||
|
||||
Specifying ``mode = Publisher`` is really just shorthand for saying
|
||||
``queryset = Publisher.objects.all()``. However, by using ``queryset``
|
||||
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`
|
||||
for more information about ``QuerySet`` objects, and see the
|
||||
:doc:`generic views reference</ref/generic-views>` for the complete details).
|
||||
|
||||
To pick a simple example, we might want to order a list of books by
|
||||
publication date, with the most recent first::
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^publishers/$', ListView.as_view(
|
||||
queryset = Publisher.objects.all(),
|
||||
context_object_name = "publisher_list",
|
||||
)),
|
||||
(r'^publishers/$', ListView.as_view(
|
||||
queryset = Book.objects.order_by("-publication_date"),
|
||||
context_object_name = "book_list",
|
||||
)),
|
||||
)
|
||||
|
||||
|
||||
That's a pretty simple example, but it illustrates the idea nicely. Of course,
|
||||
you'll usually want to do more than just reorder objects. If you want to
|
||||
present a list of books by a particular publisher, you can use the same
|
||||
technique (here, illustrated using subclassing rather than by passing arguments
|
||||
in the URLconf)::
|
||||
|
||||
from django.views.generic import ListView
|
||||
from some_app.models import Book
|
||||
|
||||
class AcmeBookListView(ListView):
|
||||
|
||||
context_object_name = "book_list"
|
||||
queryset = Book.objects.filter(publisher__name="Acme Publishing")
|
||||
template_name = "books/acme_list.html"
|
||||
|
||||
Notice that along with a filtered ``queryset``, we're also using a custom
|
||||
template name. If we didn't, the generic view would use the same template as the
|
||||
"vanilla" object list, which might not be what we want.
|
||||
|
||||
Also notice that this isn't a very elegant way of doing publisher-specific
|
||||
books. If we want to add another publisher page, we'd need another handful of
|
||||
lines in the URLconf, and more than a few publishers would get unreasonable.
|
||||
We'll deal with this problem in the next section.
|
||||
|
||||
.. note::
|
||||
|
||||
If you get a 404 when requesting ``/books/acme/``, check to ensure you
|
||||
actually have a Publisher with the name 'ACME Publishing'. Generic
|
||||
views have an ``allow_empty`` parameter for this case. See the
|
||||
:doc:`generic views reference</ref/class-based-views>` for more details.
|
||||
|
||||
|
||||
Dynamic filtering
|
||||
-----------------
|
||||
|
||||
Another common need is to filter down the objects given in a list page by some
|
||||
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
|
||||
publisher?
|
||||
|
||||
Handily, the ListView has a ``get_queryset`` method we can 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,
|
||||
various useful things are stored on ``self``; as well as the request
|
||||
(``self.request``) this includes the positional (``self.args``) and name-based
|
||||
(``self.kwargs``) arguments captured according to the URLconf.
|
||||
|
||||
Here, we have a URLconf with a single captured group::
|
||||
|
||||
from some_app.views import PublisherBookListView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^books/(\w+)/$', PublisherBookListView.as_view()),
|
||||
)
|
||||
|
||||
Next, we'll write the ``PublisherBookListView`` view itself::
|
||||
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views.generic import ListView
|
||||
from some_app.models import Book, Publisher
|
||||
|
||||
class PublisherBookListView(ListView):
|
||||
|
||||
context_object_name = "book_list"
|
||||
template_name = "books/books_by_publisher.html",
|
||||
|
||||
def get_queryset(self):
|
||||
publisher = get_object_or_404(Publisher, name__iexact=self.args[0])
|
||||
return Book.objects.filter(publisher=publisher)
|
||||
|
||||
As you can see, it's quite easy to add more logic to the queryset selection;
|
||||
if we wanted, we could use ``self.request.user`` to filter using the current
|
||||
user, or other more complex logic.
|
||||
|
||||
We can also add the publisher into the context at the same time, so we can
|
||||
use it in the template::
|
||||
|
||||
class PublisherBookListView(ListView):
|
||||
|
||||
context_object_name = "book_list"
|
||||
template_name = "books/books_by_publisher.html",
|
||||
|
||||
def get_queryset(self):
|
||||
self.publisher = get_object_or_404(Publisher, name__iexact=self.args[0])
|
||||
return Book.objects.filter(publisher=self.publisher)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
# Call the base implementation first to get a context
|
||||
context = ListView.get_context_data(self, **kwargs)
|
||||
# Add in the publisher
|
||||
context['publisher'] = self.publisher
|
||||
return context
|
||||
|
||||
Performing extra work
|
||||
---------------------
|
||||
|
||||
The last common pattern we'll look at involves doing some extra work before
|
||||
or after calling the generic view.
|
||||
|
||||
Imagine we had a ``last_accessed`` field on our ``Author`` object that we were
|
||||
using to keep track of the last time anybody looked at that author::
|
||||
|
||||
# models.py
|
||||
|
||||
class Author(models.Model):
|
||||
salutation = models.CharField(max_length=10)
|
||||
first_name = models.CharField(max_length=30)
|
||||
last_name = models.CharField(max_length=40)
|
||||
email = models.EmailField()
|
||||
headshot = models.ImageField(upload_to='/tmp')
|
||||
last_accessed = models.DateTimeField()
|
||||
|
||||
The generic ``DetailView`` class, of course, wouldn't know anything about this
|
||||
field, but once again we could easily write a custom view to keep that field
|
||||
updated.
|
||||
|
||||
First, we'd need to add an author detail bit in the URLconf to point to a
|
||||
custom view:
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
from some_app.views import AuthorDetailView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
#...
|
||||
**(r'^authors/(?P<pk>\\d+)/$', AuthorDetailView.as_view()),**
|
||||
)
|
||||
|
||||
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::
|
||||
|
||||
import datetime
|
||||
from some_app.models import Author
|
||||
from django.views.generic import DetailView
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
class AuthorDetailView(DetailView):
|
||||
|
||||
queryset = Author.objects.all()
|
||||
|
||||
def get_object(self, **kwargs):
|
||||
# Call the superclass
|
||||
object = DetailView.get_object(self, **kwargs)
|
||||
# Record the lass accessed date
|
||||
object.last_accessed = datetime.datetime.now()
|
||||
object.save()
|
||||
# Return the object
|
||||
return object
|
||||
|
||||
.. note::
|
||||
|
||||
This code won't actually work unless you create a
|
||||
``books/author_detail.html`` template.
|
||||
|
||||
.. note::
|
||||
|
||||
The URLconf here uses the named group ``pk`` - this name is the default
|
||||
name that DetailView uses to find the value of the primary key used to
|
||||
filter the queryset.
|
||||
|
||||
If you want to change it, you'll need to do your own ``get()`` call
|
||||
on ``self.queryset`` using the new named parameter from ``self.kwargs``.
|
||||
|
||||
More than just HTML
|
||||
-------------------
|
||||
|
||||
So far, we've been focusing on rendering templates to generate
|
||||
responses. However, that's not all generic views can do.
|
||||
|
||||
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
|
||||
mixins -- such as
|
||||
:class:`~django.views.generic.base.TemplateResponseMixin` -- are
|
||||
specifically designed for rendering content to a HTML response using a
|
||||
template. However, you can write your own mixins that perform
|
||||
different rendering behavior.
|
||||
|
||||
For example, you a simple JSON mixin might look something like this::
|
||||
|
||||
class JSONResponseMixin(object):
|
||||
def render_to_response(self, context):
|
||||
"Returns a JSON response containing 'context' as payload"
|
||||
return self.get_json_response(self.convert_context_to_json(context))
|
||||
|
||||
def get_json_response(self, content, **httpresponse_kwargs):
|
||||
"Construct an `HttpResponse` object."
|
||||
return http.HttpResponse(content,
|
||||
content_type='application/json',
|
||||
**httpresponse_kwargs)
|
||||
|
||||
def convert_context_to_json(self, context):
|
||||
"Convert the context dictionary into a JSON object"
|
||||
# Note: This is *EXTREMELY* naive; in reality, you'll need
|
||||
# to do much more complex handling to ensure that arbitrary
|
||||
# objects -- such as Django model instances or querysets
|
||||
# -- can be serialized as JSON.
|
||||
return json.dumps(content)
|
||||
|
||||
Then, you could build a JSON-returning
|
||||
:class:`~django.views.generic.detail.DetailView` by mixing your
|
||||
:class:`JSONResponseMixin` with the
|
||||
:class:`~django.views.generic.detail.BaseDetailView` -- (the
|
||||
:class:`~django.views.generic.detail.DetailView` before template
|
||||
rendering behavior has been mixed in)::
|
||||
|
||||
class JSONDetailView(JSONResponseMixin, BaseDetailView):
|
||||
pass
|
||||
|
||||
This view can then be deployed in the same way as any other
|
||||
:class:`~django.views.generic.detail.DetailView`, with exactly the
|
||||
same behavior -- except for the format of the response.
|
||||
|
||||
If you want to be really adventurous, you could even mix a
|
||||
:class:`~django.views.generic.detail.DetailView` subclass that is able
|
||||
to return *both* HTML and JSON content, depending on some property of
|
||||
the HTTP request, such as a query argument or a HTTP header. Just mix
|
||||
in both the :class:`JSONResponseMixin` and a
|
||||
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`,
|
||||
and override the implementation of :func:`render_to_response()` to defer
|
||||
to the appropriate subclass depending on the type of response that the user
|
||||
requested::
|
||||
|
||||
class HybridDetailView(JSONResponseMixin, SingleObjectTemplateResponseMixin, BaseDetailView):
|
||||
def render_to_response(self, context):
|
||||
# Look for a 'format=json' GET argument
|
||||
if self.request.GET.get('format','html') == 'json':
|
||||
return JSONResponseMixin.render_to_response(self, context)
|
||||
else:
|
||||
return SingleObjectTemplateResponseMixin.render_to_response(self, context)
|
||||
|
||||
Because of the way that Python resolves method overloading, the local
|
||||
:func:``render_to_response()`` implementation will override the
|
||||
versions provided by :class:`JSONResponseMixin` and
|
||||
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`.
|
||||
127
docs/topics/generic-views-migration.txt
Normal file
127
docs/topics/generic-views-migration.txt
Normal file
@@ -0,0 +1,127 @@
|
||||
======================================
|
||||
Migrating function-based generic views
|
||||
======================================
|
||||
|
||||
All the :doc:`function-based generic views</ref/generic-views>`
|
||||
that existed in Django 1.2 have analogs as :doc:`class-based generic
|
||||
views</ref/class-based-views>` in Django 1.3. The feature set
|
||||
exposed in those function-based views can be replicated in a
|
||||
class-based way.
|
||||
|
||||
How to migrate
|
||||
==============
|
||||
|
||||
Replace generic views with generic classes
|
||||
------------------------------------------
|
||||
|
||||
Existing usage of function-based generic views should be replaced with
|
||||
their class-based analogs:
|
||||
|
||||
==================================================== ====================================================
|
||||
Old function-based generic view New class-based generic view
|
||||
==================================================== ====================================================
|
||||
``django.views.generic.simple.direct_to_template`` :class:`django.views.generic.base.TemplateView`
|
||||
``django.views.generic.simple.redirect_to`` :class:`django.views.generic.base.RedirectView`
|
||||
``django.views.generic.list_detail.object_list`` :class:`django.views.generic.list.ListView`
|
||||
``django.views.generic.list_detail.object_detail`` :class:`django.views.generic.detail.DetailView`
|
||||
``django.views.generic.create_update.create_object`` :class:`django.views.generic.edit.CreateView`
|
||||
``django.views.generic.create_update.update_object`` :class:`django.views.generic.edit.UpdateView`
|
||||
``django.views.generic.create_update.delete_object`` :class:`django.views.generic.edit.DeleteView`
|
||||
``django.views.generic.date_based.archive_index`` :class:`django.views.generic.dates.ArchiveIndexView`
|
||||
``django.views.generic.date_based.archive_year`` :class:`django.views.generic.dates.YearArchiveView`
|
||||
``django.views.generic.date_based.archive_month`` :class:`django.views.generic.dates.MonthArchiveView`
|
||||
``django.views.generic.date_based.archive_week`` :class:`django.views.generic.dates.WeekArchiveView`
|
||||
``django.views.generic.date_based.archive_day`` :class:`django.views.generic.dates.DayArchiveView`
|
||||
``django.views.generic.date_based.archive_today`` :class:`django.views.generic.dates.TodayArchiveView`
|
||||
``django.views.generic.date_based.object_detail`` :class:`django.views.generic.dates.DateDetailView`
|
||||
==================================================== ====================================================
|
||||
|
||||
To do this, replace the reference to the generic view function with
|
||||
a ``as_view()`` instantiation of the class-based view. For example,
|
||||
the old-style ``direct_to_template`` pattern::
|
||||
|
||||
('^about/$', direct_to_template, {'template': 'about.html'})
|
||||
|
||||
can be replaced with an instance of
|
||||
:class:`~django.views.generic.base.TemplateView`::
|
||||
|
||||
('^about/$', TemplateView.as_view(template_name='about.html'))
|
||||
|
||||
``template`` argument to ``direct_to_template`` views
|
||||
-----------------------------------------------------
|
||||
|
||||
The ``template`` argument to the ``direct_to_template`` view has been renamed
|
||||
``template_name``. This has ben done to maintain consistency with other views.
|
||||
|
||||
``object_id`` argument to detail views
|
||||
--------------------------------------
|
||||
|
||||
The object_id argument to the ``object_detail`` view has been renamed
|
||||
``pk`` on the :class:`~django.views.generic.detail.DetailView`.
|
||||
|
||||
``template_object_name``
|
||||
------------------------
|
||||
|
||||
``template_object_name`` has been renamed ``context_object_name``,
|
||||
reflecting the fact that the context data can be used for purposes
|
||||
other than template rendering (e.g., to populate JSON output).
|
||||
|
||||
The ``_list`` suffix on list views
|
||||
----------------------------------
|
||||
|
||||
In a function-based :class:`ListView`, the ``template_object_name``
|
||||
was appended with the suffix ``'_list'`` to yield the final context
|
||||
variable name. In a class-based ``ListView``, the
|
||||
``context_object_name`` is used verbatim.
|
||||
|
||||
``extra_context``
|
||||
-----------------
|
||||
|
||||
Function-based generic views provided an ``extra_context`` argument
|
||||
as way to insert extra items into the context at time of rendering.
|
||||
|
||||
Class-based views don't provide an ``extra_context`` argument.
|
||||
Instead, you subclass the view, overriding :meth:`get_context_data()`.
|
||||
For example::
|
||||
|
||||
class MyListView(ListView):
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(MyListView, self).get_context_data(**kwargs)
|
||||
context.update({
|
||||
'foo': 42,
|
||||
'bar': 37
|
||||
})
|
||||
return context
|
||||
|
||||
``mimetype``
|
||||
------------
|
||||
|
||||
Some function-based generic views provided a ``mimetype`` argument
|
||||
as way to control the mimetype of the response.
|
||||
|
||||
Class-based views don't provide a ``mimetype`` argument. Instead, you
|
||||
subclass the view, overriding
|
||||
:meth:`TemplateResponseMixin.get_response()` and pass in arguments for
|
||||
the HttpResponse constructor. For example::
|
||||
|
||||
class MyListView(ListView):
|
||||
def get_response(self, content, **kwargs):
|
||||
return super(MyListView, self).get_response(content,
|
||||
content_type='application/json', **kwargs)
|
||||
|
||||
``context_processors``
|
||||
----------------------
|
||||
|
||||
Some function-based generic views provided a ``context_processors``
|
||||
argument that could be used to force the use of specialized context
|
||||
processors when rendering template content.
|
||||
|
||||
Class-based views don't provide a ``context_processors`` argument.
|
||||
Instead, you subclass the view, overriding
|
||||
:meth:`TemplateResponseMixin.get_context_instance()`. For example::
|
||||
|
||||
class MyListView(ListView):
|
||||
def get_context_instance(self, context):
|
||||
return RequestContext(self.request,
|
||||
context,
|
||||
processors=[custom_processor])
|
||||
@@ -12,7 +12,8 @@ Introductions to all the key parts of Django you'll need to know:
|
||||
forms/index
|
||||
forms/modelforms
|
||||
templates
|
||||
generic-views
|
||||
class-based-views
|
||||
generic-views-migration
|
||||
files
|
||||
testing
|
||||
auth
|
||||
@@ -26,3 +27,10 @@ Introductions to all the key parts of Django you'll need to know:
|
||||
settings
|
||||
signals
|
||||
|
||||
Deprecated features
|
||||
-------------------
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
generic-views
|
||||
|
||||
Reference in New Issue
Block a user