← Django Foundations
9 / 25
Lesson 9 — Pagination
One page at a time
Pagination splits a long list into pages. With a page size of 2, page 1 is items 0–1, page 2 is items 2–3, and so on. The slice for page p starts at (p - 1) * size and stops at p * size:
items[2:4] # page 2 when size is 2
Django has a Paginator class that does exactly this math for you.
Note: the exercise runs in your browser without Django installed, so it is a small plain-Python simulation of what Django does for you.
Why it matters: nobody can load 10,000 products at once — pages keep sites fast.
Want it another way?
Task
Finish page() so it returns the items for page p. Pages 1, 2 and 4 should print as shown.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.