The Paginator Class
Break a queryset into pages.
The Paginator Class is a free Django Academy lesson on CoddyKit — lesson 1 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.
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. 🎉
Frequently asked questions
Is the “The Paginator Class” lesson free?
Yes — the full text of “The Paginator Class” 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 “The Paginator Class”?
Break a queryset into pages. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Paginator Class” 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.