Seitennavigation in ListView
Links für nächste und vorherige Seiten in Templates hinzufügen
Seitennavigation in ListView ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎉
Häufig gestellte Fragen
Ist die Lektion „Seitennavigation in ListView“ kostenlos?
Ja — der vollständige Text von „Seitennavigation in ListView“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Seitennavigation in ListView“?
Links für nächste und vorherige Seiten in Templates hinzufügen Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Seitennavigation in ListView“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Die Paginator-Klasse
- Seitennavigation in ListView
- TestCase und Assertions schreiben
- Der Test Client und Fixtures