Problem statement
The article listing is a ListView in name only: it knows which model to read and nothing else. Django's defaults then guess a template that does not exist, publish the list under a name the page does not loop over, return rows in whatever order the database offers, and hand over all of them at once because nothing said to paginate. Four attributes close all four gaps.
Requirements
Implement:
- the configuration of ArticleListView in app/views.py
The following files are already implemented and provided: app/models.py, app/urls.py and the template. Article holds title, slug, body and published_on. /articles/ is routed to ArticleListView.as_view() under the name article-list. The page, read-only at app/templates/app/articles.html, loops over articles and reads page_obj and is_paginated for its navigation strip. ArticleListView already sets model = Article and nothing else: a generic view is configuration more than code, and what it cannot know is your naming and your policy.
1. The template and the context name
- The view renders app/articles.html. Without being told, Django guesses <app>/<model>_list.html, which is not the file this project has.
- The list is published in the context as articles. Without being told, it arrives as object_list and <model>_list, and a template looping over anything else sees nothing.
2. The order
- Newest first by published_on, and two articles sharing a date tie-break by title ascending, so the newest page leads with Aardvark before Article 11.
3. Pagination
- Five articles per page, so thirteen articles make three pages and the last one holds the remaining three.
- The context then exposes page_obj, paginator and is_paginated, with page_obj.number, page_obj.paginator.num_pages, has_previous() and has_next() all answering for the page that was asked for, and the strip rendering its Page 2 of 3 state.
- ?page= is read by the view itself, and a page that does not exist is a 404 rather than a fallback page of rows - 4 past the end, 0, and a non-numeric frog alike. That part is free once pagination is switched on, and it is the behaviour the hand-rolled version kept getting wrong.
Notes
- Only app/views.py changes.
- Configure the generic view: four attributes close all four gaps. Do not override get, get_queryset or get_context_data, and do not paginate by hand.
- ordering takes a tuple, with - for descending and later fields breaking ties.
- Leave home as it is.
