0
0
Djangoframework~10 mins

Pagination (PageNumber, Cursor, Limit/Offset) in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pagination (PageNumber, Cursor, Limit/Offset)
Start Request
Extract Pagination Params
Limit/Offset
Calculate Slice
PageNumber
Calculate Page Range
Cursor
Use Cursor to Fetch
Query Database with Params
Return Paginated Results
End
The server receives pagination parameters (PageNumber, Cursor, or Limit/Offset), calculates which data slice to fetch, queries the database, and returns the paginated results.
Execution Sample
Django
from django.core.paginator import Paginator

items = list(range(1, 21))
paginator = Paginator(items, 5)
page = paginator.page(2)
print(page.object_list)
This code paginates a list of 20 items into pages of 5 items each and prints the items on page 2.
Execution Table
StepActionInput/StateResult/Output
1Create list of itemsitems = [1..20]List with 20 numbers created
2Create PaginatorPaginator(items, 5)Paginator with 4 pages (5 items each)
3Request page 2page = paginator.page(2)Page object for page 2 created
4Get items on page 2page.object_list[6, 7, 8, 9, 10]
5Print itemsprint(page.object_list)Outputs: [6, 7, 8, 9, 10]
6EndNo more stepsPagination complete
💡 Reached requested page 2, output items sliced accordingly
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
itemsundefined[1,2,...,20][1,2,...,20][1,2,...,20][1,2,...,20][1,2,...,20]
paginatorundefinedundefinedPaginator object (4 pages)Paginator object (4 pages)Paginator object (4 pages)Paginator object (4 pages)
pageundefinedundefinedundefinedPage 2 objectPage 2 objectPage 2 object
page.object_listundefinedundefinedundefinedundefined[6,7,8,9,10][6,7,8,9,10]
Key Moments - 3 Insights
Why does page 2 return items starting from 6, not 5?
Because pagination pages start counting items from 1, page 1 has items 1-5, so page 2 starts at item 6 as shown in execution_table row 4.
What happens if I request a page number beyond the last page?
Django's Paginator raises an exception if the page number is too high. This is not shown here but you must handle it to avoid errors.
How does Limit/Offset differ from PageNumber pagination?
Limit/Offset uses a start position and count to slice data directly, while PageNumber uses page count and size to calculate slices, as seen in the concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of page.object_list at step 4?
A[1, 2, 3, 4, 5]
B[6, 7, 8, 9, 10]
C[11, 12, 13, 14, 15]
D[16, 17, 18, 19, 20]
💡 Hint
Check the 'Result/Output' column at step 4 in the execution_table
At which step does the paginator know how many pages exist?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Result/Output' columns in execution_table step 2
If the page size changes from 5 to 10, how does page 2's object_list change?
AIt will contain items [1, 2, ..., 10]
BIt will contain items [6, 7, 8, 9, 10]
CIt will contain items [11, 12, ..., 20]
DIt will be empty
💡 Hint
Refer to variable_tracker and how page size affects slicing in the concept_flow
Concept Snapshot
Django Pagination:
- Use Paginator(items, per_page) to split data
- Access pages with paginator.page(number)
- page.object_list gives items on that page
- PageNumber pagination uses page count
- Limit/Offset slices by start and count
- Cursor pagination uses a pointer for next data slice
Full Transcript
This visual execution shows how Django pagination works using PageNumber style. First, a list of 20 items is created. Then, a Paginator object is made with 5 items per page, resulting in 4 pages. Requesting page 2 returns items 6 to 10. The execution table traces each step from list creation to printing the page items. Variables like items, paginator, page, and page.object_list are tracked through the steps. Key moments clarify why page 2 starts at item 6 and differences between pagination types. The quiz tests understanding of output at steps, paginator state, and effects of changing page size.