summaryrefslogtreecommitdiff
path: root/docs/intro/tutorial04.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/intro/tutorial04.txt')
-rw-r--r--docs/intro/tutorial04.txt70
1 files changed, 42 insertions, 28 deletions
diff --git a/docs/intro/tutorial04.txt b/docs/intro/tutorial04.txt
index 87d8e584ad..9f54243a3e 100644
--- a/docs/intro/tutorial04.txt
+++ b/docs/intro/tutorial04.txt
@@ -185,7 +185,7 @@ conversion. We will:
2. Delete some of the old, unneeded views.
-3. Fix up URL handling for the new views.
+3. Introduce new views based on Django's generic views.
Read on for details.
@@ -205,32 +205,51 @@ Amend URLconf
First, open the ``polls/urls.py`` URLconf and change it like so::
from django.conf.urls import patterns, url
- from django.views.generic import DetailView, ListView
- from polls.models import Poll
+
+ from polls import views
urlpatterns = patterns('',
- url(r'^$',
- ListView.as_view(
- queryset=Poll.objects.order_by('-pub_date')[:5],
- context_object_name='latest_poll_list',
- template_name='polls/index.html'),
- name='index'),
- url(r'^(?P<pk>\d+)/$',
- DetailView.as_view(
- model=Poll,
- template_name='polls/detail.html'),
- name='detail'),
- url(r'^(?P<pk>\d+)/results/$',
- DetailView.as_view(
- model=Poll,
- template_name='polls/results.html'),
- name='results'),
- url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote', name='vote'),
+ url(r'^$', views.IndexView.as_view(), name='index'),
+ url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
+ url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
+ url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
Amend views
-----------
+Next, we're going to remove our old ``index``, ``detail``, and ``results``
+views and use Django's generic views instead. To do so, open the
+``polls/views.py`` file and change it like so::
+
+ from django.shortcuts import get_object_or_404, render
+ from django.http import HttpResponseRedirect
+ from django.core.urlresolvers import reverse
+ from django.views import generic
+
+ from polls.models import Choice, Poll
+
+ class IndexView(generic.ListView):
+ template_name = 'polls/index.html'
+ context_object_name = 'latest_poll_list'
+
+ def get_queryset(self):
+ """Return the last five published polls."""
+ return Poll.objects.order_by('-pub_date')[:5]
+
+
+ class DetailView(generic.DetailView):
+ model = Poll
+ template_name = 'polls/detail.html'
+
+
+ class ResultsView(generic.DetailView):
+ model = Poll
+ template_name = 'polls/results.html'
+
+ def vote(request, poll_id):
+ ....
+
We're using two generic views here:
:class:`~django.views.generic.list.ListView` and
:class:`~django.views.generic.detail.DetailView`. Respectively, those
@@ -238,7 +257,7 @@ two views abstract the concepts of "display a list of objects" and
"display a detail page for a particular type of object."
* Each generic view needs to know what model it will be acting
- upon. This is provided using the ``model`` parameter.
+ upon. This is provided using the ``model`` attribute.
* The :class:`~django.views.generic.detail.DetailView` generic view
expects the primary key value captured from the URL to be called
@@ -248,7 +267,7 @@ two views abstract the concepts of "display a list of objects" and
By default, the :class:`~django.views.generic.detail.DetailView` generic
view uses a template called ``<app name>/<model name>_detail.html``.
In our case, it'll use the template ``"polls/poll_detail.html"``. The
-``template_name`` argument is used to tell Django to use a specific
+``template_name`` attribute is used to tell Django to use a specific
template name instead of the autogenerated default template name. We
also specify the ``template_name`` for the ``results`` list view --
this ensures that the results view and the detail view have a
@@ -268,16 +287,11 @@ automatically -- since we're using a Django model (``Poll``), Django
is able to determine an appropriate name for the context variable.
However, for ListView, the automatically generated context variable is
``poll_list``. To override this we provide the ``context_object_name``
-option, specifying that we want to use ``latest_poll_list`` instead.
+attribute, specifying that we want to use ``latest_poll_list`` instead.
As an alternative approach, you could change your templates to match
the new default context variables -- but it's a lot easier to just
tell Django to use the variable you want.
-You can now delete the ``index()``, ``detail()`` and ``results()`` views from
-``polls/views.py``. We don't need them anymore -- they have been replaced by
-generic views. You can also delete the import for ``HttpResponse``, which is no
-longer required.
-
Run the server, and use your new polling app based on generic views.
For full details on generic views, see the :doc:`generic views documentation