1
0
mirror of https://github.com/django/django.git synced 2025-02-28 19:44:35 +00:00

[4.2.x] Reorganized tutorial's part 4 to better understand changes needed in URLConf.

Backport of d21ab70223dcbdc5a2603a88945147f2f780deee from main
This commit is contained in:
Natalia 2023-05-25 11:18:26 -03:00
parent 0cbc92bc3a
commit 830990fa6c

View File

@ -215,8 +215,12 @@ the database according to a parameter passed in the URL, loading a template and
returning the rendered template. Because this is so common, Django provides a returning the rendered template. Because this is so common, Django provides a
shortcut, called the "generic views" system. shortcut, called the "generic views" system.
Generic views abstract common patterns to the point where you don't even need Generic views abstract common patterns to the point where you don't even need to
to write Python code to write an app. write Python code to write an app. For example, the
:class:`~django.views.generic.list.ListView` and
:class:`~django.views.generic.detail.DetailView` generic views
abstract the concepts of "display a list of objects" and
"display a detail page for a particular type of object" respectively.
Let's convert our poll app to use the generic views system, so we can delete a Let's convert our poll app to use the generic views system, so we can delete a
bunch of our own code. We'll have to take a few steps to make the conversion. 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:
] ]
Note that the name of the matched pattern in the path strings of the second and Note that the name of the matched pattern in the path strings of the second and
third patterns has changed from ``<question_id>`` to ``<pk>``. third patterns has changed from ``<question_id>`` to ``<pk>``. This is
necessary because we'll use the
:class:`~django.views.generic.detail.DetailView` generic view to replace our
``detail()`` and ``results()`` views, and it expects the primary key value
captured from the URL to be called ``"pk"``.
Amend views Amend views
----------- -----------
@ -303,19 +311,11 @@ views and use Django's generic views instead. To do so, open the
def vote(request, question_id): def vote(request, question_id):
... # same as above, no changes needed. ... # same as above, no changes needed.
We're using two generic views here: Each generic view needs to know what model it will be acting upon. This is
:class:`~django.views.generic.list.ListView` and provided using either the ``model`` attribute (in this example, ``model =
:class:`~django.views.generic.detail.DetailView`. Respectively, those Question`` for ``DetailView`` and ``ResultsView``) or by defining the
two views abstract the concepts of "display a list of objects" and :meth:`~django.views.generic.list.MultipleObjectMixin.get_queryset` method (as
"display a detail page for a particular type of object." shown in ``IndexView``).
* Each generic view needs to know what model it will be acting
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
``"pk"``, so we've changed ``question_id`` to ``pk`` for the generic
views.
By default, the :class:`~django.views.generic.detail.DetailView` generic By default, the :class:`~django.views.generic.detail.DetailView` generic
view uses a template called ``<app name>/<model name>_detail.html``. view uses a template called ``<app name>/<model name>_detail.html``.