7.9 DetailView and ListView
The two plainest generic views that show a model’s objects are DetailView, which finds one object by what the path captured, with the methods of SingleObjectMixin, and ListView, which lists many with those of MultipleObjectMixin and divides them into pages when paginate_by is set.
Each of these views is two halves, a base view that has the handler method and a response mixin that makes the response, and its decisions are taken by methods of mixins (The generic views). By default both of these views find their objects through get_queryset, from the queryset attribute or else the model attribute’s default manager, put what they find in the context under a name made from the model, and name a template made from the model too.
DetailView: one object
The Gazette’s view of one article names a model and nothing else.
# gazette/views.py
class ArticleDetail(DetailView):
model = Article
# gazette/urls.py
path("articles/<slug:slug>/", views.ArticleDetail.as_view(), name="article"),
BaseDetailView finds the object and keeps it as BaseDetailView, builds the context, and makes the response. Each step is a method found through the method resolution order, and the figure sets six methods of a "GET" against the classes DetailView inherits from: the two methods that run in two classes are the context’s and the template names’.
DetailView’s get. The handler method is in one class, the lookup in a second, the response in a third, and two of the steps pass through two classes by way of super().What the figure cannot show is the request itself: the SQL that finds the article. For GET / the three groups under get are the three steps, and the ImproperlyConfigured near the end is an answer, not a failure, since SingleObjectTemplateResponseMixin catches it and builds the name itself:
GET /articles/ferry-timetable/: a DetailView
...
BaseDetailView.get(request, slug='ferry-timetable')
SingleObjectMixin.get_object()
SingleObjectMixin.get_queryset -> a QuerySet of Article, not yet fetched
SingleObjectMixin.get_slug_field -> 'slug'
SQL SELECT "gazette_article"."id", "gazette_article"."slug", "gazette_article"."headline", "gazette_article"."published" FROM "gazette_article" WHERE "gazette_article"."slug" = %s LIMIT 21 with params ('ferry-timetable',)
-> <Article: New ferry timetable>
SingleObjectMixin.get_context_data(object=...)
SingleObjectMixin.get_context_object_name(obj) -> 'article'
ContextMixin.get_context_data(object=..., article=...) -> a dict with the keys article, object, view
-> a dict with the keys article, object, view
TemplateResponseMixin.render_to_response(context)
SingleObjectTemplateResponseMixin.get_template_names
TemplateResponseMixin.get_template_names raises ImproperlyConfigured
-> ['gazette/article_detail.html']
...
The object. SingleObjectMixin starts from a queryset and narrows it with what the path captured. It reads two keyword arguments from View.kwargs, named by the attributes SingleObjectMixin and SingleObjectMixin, which are "pk" and "slug". With a primary key it filters on that, and ignores a slug unless query_pk_and_slug is true, in which case it filters on both. With only a slug it filters on the field that get_slug_field names. With neither it raises AttributeError, whose message says the view must be called with one or the other in the URLconf.
Then it calls get on what is left. The LIMIT 21 in the query is that method’s own doing, not the view’s. An object that does not exist raises the model’s Model, and get_object raises Http404 in its place, which the handler ordinarily turns into a 404 (How an exception becomes a response):
GET /articles/no-such-article/: the DetailView finds no object
...
SingleObjectMixin.get_object()
...
SQL SELECT "gazette_article"."id", "gazette_article"."slug", "gazette_article"."headline", "gazette_article"."published" FROM "gazette_article" WHERE "gazette_article"."slug" = %s LIMIT 21 with params ('no-such-article',)
raises Http404
response_for_exception(request, Http404) -> an HttpResponseNotFound, status 404
...
The queryset. SingleObjectMixin returns the queryset attribute if one is set, and otherwise everything the model attribute’s default manager has (Managers); with neither it raises ImproperlyConfigured. In both cases what it returns is the result of all(), a new queryset.
The context. SingleObjectMixin puts the view’s object, when it has one, in as "object", and a second time under the name get_context_object_name returns, when it returns one: context_object_name if set, and otherwise, for a model instance, the model’s name in lower case. So the Gazette’s template can say "article" where a template written for any model would say "object".
The template. With no template_name, SingleObjectTemplateResponseMixin builds a list. If template_name_field is set, the value of that field on the object, when it has one, goes first: the object names its own template. Then comes a name made from the object’s model, or from the view’s model when the object is not a model instance: the app label as a directory, and the model’s name with template_name_suffix, which is "_detail". If nothing could be built it raises ImproperlyConfigured again, with a message that lists what would have helped.
ListView: many objects, and pages of them
The Gazette lists its articles three to a page, newest first.
# gazette/views.py
class ArticleList(ListView):
model = Article
paginate_by = 3
ordering = "-published"
BaseListView.get keeps the queryset as BaseListView, makes the context and makes the response. For the second page, GET /, the rows are counted under Paginator.page, while the context is being made, since a page number cannot be validated without knowing how many rows there are. The rows themselves are fetched only under SimpleTemplateResponse, after the view has returned, by the template’s own loop: what the view put in the context was a queryset limited to three rows and not yet run (QuerySets):
GET /articles/?page=2: a ListView that paginates
...
BaseListView.get(request)
MultipleObjectMixin.get_queryset
MultipleObjectMixin.get_ordering -> '-published'
-> a QuerySet of Article, ordered by -published, not yet fetched
...
MultipleObjectMixin.get_context_data()
MultipleObjectMixin.get_paginate_by(queryset) -> 3
MultipleObjectMixin.get_context_object_name(object_list) -> 'article_list'
MultipleObjectMixin.paginate_queryset(queryset, 3)
...
Paginator.page(2)
SQL SELECT COUNT(*) AS "__count" FROM "gazette_article" with params ()
-> <Page 2 of 3>
-> (a Paginator, <Page 2 of 3>, the page's objects: a QuerySet of Article, ordered by -published, not yet fetched, True)
ContextMixin.get_context_data(paginator=..., page_obj=..., is_paginated=..., object_list=..., article_list=...) -> a dict with the keys article_list, is_paginated, object_list, page_obj, paginator, view
...
SimpleTemplateResponse.render
SQL SELECT "gazette_article"."id", "gazette_article"."slug", "gazette_article"."headline", "gazette_article"."published" FROM "gazette_article" ORDER BY "gazette_article"."published" DESC LIMIT 3 OFFSET 3 with params ()
...
The queryset. MultipleObjectMixin differs from the single-object one in two ways. The queryset attribute may be any iterable, a list for instance, and all() is called on it only when it is a QuerySet. And an ordering is applied at the end when there is one: get_ordering returns the ordering attribute, which is None unless it has been set to a field name or a sequence of them, and when it is set the queryset is passed through order_by.
Pages. MultipleObjectMixin works on the list get kept. A list passed to it as object_list takes that one’s place, whereas its other keyword arguments are only laid over the context it builds. It asks get_paginate_by for a page size. If the answer is nothing, the whole list goes into the context as "object_list", with "paginator" and "page_obj" as None and "is_paginated" false. Otherwise paginate_queryset takes over, and the context holds the paginator, the page as "page_obj", the page’s own objects as "object_list", and "is_paginated", which is true when there is more than one page. As in the detail view, the same objects are in the context a second time when get_context_object_name returns a name: context_object_name if set, and otherwise, for a list that has a model, the model’s name with "_list" after it.
paginate_queryset makes a Paginator and looks for the page number under the name page_kwarg, which is "page", first in the path’s keyword arguments and then in the query string, taking 1 when neither gives one. A value that int refuses, other than the word "last", is an Http404, and so is a number the paginator refuses. Page 9 of the Gazette’s three is such a number: Paginator.page counts the rows and raises EmptyPage, and paginate_queryset raises Http404 in its place:
GET /articles/?page=9: a page the list does not have
...
Paginator.page(9)
SQL SELECT COUNT(*) AS "__count" FROM "gazette_article" with params ()
raises EmptyPage
raises Http404
response_for_exception(request, Http404) -> an HttpResponseNotFound, status 404
...
An empty list. By default an empty list is rendered like any other. With allow_empty false, BaseListView.get raises Http404 for one, and takes care how it finds out: when get_paginate_by gives anything but None and the list is a queryset, it asks exists, a cheap query, where otherwise it tests the list for truth, which loads a queryset whole.
The template. MultipleObjectTemplateResponseMixin takes template_name if there is one and then, when the list has a model, adds a name built from it with the suffix "_list". Unlike the detail view’s, this one adds the generated name even when template_name was given, so the given name is tried first and the generated one stands behind it.