Page Controls in ListView
Add next/previous links in templates.
Page Controls in ListView is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Pagination Built In
Good news: ListView can paginate for you. You barely write any code, and Django handles slicing the queryset into pages automatically. 🚀
Set paginate_by
Just add one attribute. Setting paginate_by tells the ListView how many items each page should show.
class ArticleList(ListView):
model = Article
paginate_by = 10The page_obj Variable
With pagination on, your template receives a page_obj. It is the same Page object you met before, ready to loop and inspect.
Looping the Current Page
In the template, iterate page_obj to show only this page's items. Django already sliced the queryset for you.
{% for article in page_obj %}
{{ article.title }}
{% endfor %}Reading the Page Number
ListView pulls the page number from the ?page= query string in the URL, so a link to ?page=2 shows the second page.
Show a Previous Link
Guard the back link with has_previous so it only appears when there really is an earlier page to visit.
{% if page_obj.has_previous %}
Prev
{% endif %}Link to the Previous Page
Inside that block, build the href using previous_page_number so the link points one page back.
<a href="?page={{ page_obj.previous_page_number }}">Prev</a>Show a Next Link
Mirror the logic for forward navigation. Wrap the next link in a has_next check so it hides on the final page.
{% if page_obj.has_next %}
Next
{% endif %}Display Page X of Y
Reassure users with a counter. Combine number and the paginator's num_pages to print "Page 2 of 5".
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}Custom Context Name
Prefer a friendlier loop variable? Set context_object_name and Django names the page list whatever you choose.
context_object_name = 'articles'Putting It Together
One attribute plus a small template block gives you full page controls: prev, next, and a position counter, with no view logic to write.
Quick Check
Your ListView template needs the current page's items.
Recap
You enabled pagination with paginate_by, looped page_obj, and built prev/next links guarded by has_previous and has_next. Clean lists, zero view code. 🎉
Frequently asked questions
Is the “Page Controls in ListView” lesson free?
Yes — the full text of “Page Controls in ListView” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Page Controls in ListView”?
Add next/previous links in templates. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Page Controls in ListView” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Paginator Class
- Page Controls in ListView
- Writing TestCase and assertions
- The Test Client and Fixtures