@@ -215,8 +215,12 @@ the database according to a parameter passed in the URL, loading a template and
215215returning the rendered template. Because this is so common, Django provides a
216216shortcut, called the "generic views" system.
217217
218- Generic views abstract common patterns to the point where you don't even need
219- to write Python code to write an app.
218+ Generic views abstract common patterns to the point where you don't even need to
219+ write Python code to write an app. For example, the
220+ :class:`~django.views.generic.list.ListView` and
221+ :class:`~django.views.generic.detail.DetailView` generic views
222+ abstract the concepts of "display a list of objects" and
223+ "display a detail page for a particular type of object" respectively.
220224
221225Let's convert our poll app to use the generic views system, so we can delete a
222226bunch of our own code. We'll have to take a few steps to make the conversion.
@@ -261,7 +265,11 @@ First, open the ``polls/urls.py`` URLconf and change it like so:
261265 ]
262266
263267Note that the name of the matched pattern in the path strings of the second and
264- third patterns has changed from ``<question_id>`` to ``<pk>``.
268+ third patterns has changed from ``<question_id>`` to ``<pk>``. This is
269+ necessary because we'll use the
270+ :class:`~django.views.generic.detail.DetailView` generic view to replace our
271+ ``detail()`` and ``results()`` views, and it expects the primary key value
272+ captured from the URL to be called ``"pk"``.
265273
266274Amend views
267275-----------
@@ -303,19 +311,11 @@ views and use Django's generic views instead. To do so, open the
303311 def vote(request, question_id):
304312 ... # same as above, no changes needed.
305313
306- We're using two generic views here:
307- :class:`~django.views.generic.list.ListView` and
308- :class:`~django.views.generic.detail.DetailView`. Respectively, those
309- two views abstract the concepts of "display a list of objects" and
310- "display a detail page for a particular type of object."
311-
312- * Each generic view needs to know what model it will be acting
313- upon. This is provided using the ``model`` attribute.
314-
315- * The :class:`~django.views.generic.detail.DetailView` generic view
316- expects the primary key value captured from the URL to be called
317- ``"pk"``, so we've changed ``question_id`` to ``pk`` for the generic
318- views.
314+ Each generic view needs to know what model it will be acting upon. This is
315+ provided using either the ``model`` attribute (in this example, ``model =
316+ Question`` for ``DetailView`` and ``ResultsView``) or by defining the
317+ :meth:`~django.views.generic.list.MultipleObjectMixin.get_queryset` method (as
318+ shown in ``IndexView``).
319319
320320By default, the :class:`~django.views.generic.detail.DetailView` generic
321321view uses a template called ``<app name>/<model name>_detail.html``.
0 commit comments