Controles de página no ListView
Adicione links de próxima e anterior nos templates
Controles de página no ListView é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 🎉
Aprenda Python com um tutor de IA — grátis
Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.
- Cursos
- 30
- Aulas
- 120
Perguntas Frequentes
A aula “Controles de página no ListView” é grátis?
Sim — o texto completo de “Controles de página no ListView” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.
O que vou aprender em “Controles de página no ListView”?
Adicione links de próxima e anterior nos templates Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Django Academy?
Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Controles de página no ListView”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Django Academy?
Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- A classe Paginator
- Controles de página no ListView
- Escrevendo TestCase e asserções
- O cliente de testes e fixtures