Skip to content

Commit d21ab70

Browse files
committed
Reorganized tutorial's part 4 to better understand changes needed in URLConf.
1 parent e083f30 commit d21ab70

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

docs/intro/tutorial04.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,12 @@ the database according to a parameter passed in the URL, loading a template and
215215
returning the rendered template. Because this is so common, Django provides a
216216
shortcut, 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

221225
Let's convert our poll app to use the generic views system, so we can delete a
222226
bunch 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

263267
Note 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

266274
Amend 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

320320
By default, the :class:`~django.views.generic.detail.DetailView` generic
321321
view uses a template called ``<app name>/<model name>_detail.html``.

0 commit comments

Comments
 (0)