La classe Paginator
Suddividere un queryset in pagine
La classe Paginator è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Why Paginate?
Showing 5,000 rows on one page is slow and overwhelming. Pagination splits a long list into bite-sized pages your users can browse. 📄
Meet the Paginator
Django ships a Paginator class that does the math for you. You hand it a list and a page size, and it carves the data into pages.
from django.core.paginator import PaginatorCreate a Paginator
Pass your full queryset and how many items per page. Here every page holds 10 articles.
articles = Article.objects.all()
paginator = Paginator(articles, 10)Counting the Pages
Ask the paginator how many pages exist with num_pages. It rounds up, so 25 items at 10 per page gives 3 pages.
paginator.num_pages # 3Grab One Page
Call get_page() with a page number to fetch a single slice. It returns a Page object holding just those items.
page = paginator.get_page(2)Looping Over a Page
A Page is iterable, so you loop it like a list. You only ever touch the items on that one page, never all 5,000.
for article in page:
print(article.title)get_page Is Forgiving
If someone passes page 999 or the word "abc", get_page() quietly returns the last or first valid page instead of crashing.
Knowing Where You Are
The page knows its own spot. Read number for the current page, handy for highlighting it in your navigation bar.
page.number # 2Next and Previous
Use has_next() and has_previous() to decide whether to show the next or back link in your template.
page.has_next()
page.has_previous()The Neighboring Pages
Once you know there is a next page, next_page_number() gives you the number to link to. previous_page_number() does the same backward.
page.next_page_number() # 3Wiring It Into a View
In a view, read the requested page from the query string, then pass the page to your template context.
num = request.GET.get('page')
page = paginator.get_page(num)Quick Check
You have 23 items and a page size of 10. Let's test the math.
Recap
You met the Paginator: feed it a queryset and a page size, fetch slices with get_page(), and read num_pages, number, and has_next() to build navigation. 🎉
Domande Frequenti
La lezione «La classe Paginator» è gratuita?
Sì — il testo completo di «La classe Paginator» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.
Cosa imparerò in «La classe Paginator»?
Suddividere un queryset in pagine Eserciti Django Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Django Academy?
Non è richiesta alcuna esperienza precedente. Django Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «La classe Paginator»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Django Academy?
Sì. Ogni lezione Django Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- La classe Paginator
- Controlli di pagina in ListView
- Scrivere TestCase e asserzioni
- Il test client e i fixture