0
0
Djangoframework~20 mins

Pagination (PageNumber, Cursor, Limit/Offset) in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pagination Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django PageNumberPagination response?

Given a Django REST Framework view using PageNumberPagination with page_size=3, what will the JSON response contain when requesting page 2?

Django
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

class MyPagination(PageNumberPagination):
    page_size = 3

# Assume queryset has 7 items indexed 1 to 7

# Request: GET /items/?page=2

# What will be the 'results' length in the response?
AThe 'results' list will contain 3 items (items 4, 5, 6).
BThe 'results' list will contain 1 item (item 7 only).
CThe 'results' list will contain 4 items (items 4, 5, 6, 7).
DThe 'results' list will be empty because page 2 is invalid.
Attempts:
2 left
💡 Hint

PageNumberPagination divides items into pages of fixed size. Page 1 has items 1-3, page 2 has items 4-6, and so on.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets up CursorPagination in Django REST Framework?

Choose the correct way to define a cursor pagination class with a page size of 5 and ordering by 'created' field.

A
class MyCursorPagination(CursorPagination):
    page_size = 5
    ordering = 'created'
B
class MyCursorPagination(CursorPagination):
    page_size = 5
    ordering = created
C
class MyCursorPagination(CursorPagination):
    page_size = 5
    ordering = ('created')
D
class MyCursorPagination(CursorPagination):
    page_size = 5
    ordering = ['created']
Attempts:
2 left
💡 Hint

The ordering attribute expects an iterable of strings.

🔧 Debug
advanced
2:00remaining
Why does this LimitOffsetPagination code raise a TypeError?

Examine the following Django REST Framework pagination class and identify why it raises a TypeError when used.

Django
from rest_framework.pagination import LimitOffsetPagination

class MyLimitOffsetPagination(LimitOffsetPagination):
    default_limit = '10'

# Using this pagination causes TypeError: unsupported operand type(s) for -: 'str' and 'int'
AThe class must define a max_limit attribute to avoid errors.
BLimitOffsetPagination requires overriding the paginate_queryset method.
Cdefault_limit is set as a string instead of an integer.
DThe TypeError is caused by missing a call to super().__init__() in the class.
Attempts:
2 left
💡 Hint

Check the type of default_limit and how it is used internally.

state_output
advanced
2:00remaining
What is the value of 'next' in this PageNumberPagination response?

Given a queryset of 12 items and PageNumberPagination with page_size=5, what will the 'next' URL be when requesting page 2?

Django
# Request: GET /api/items/?page=2
# Page size: 5
# Total items: 12

# What is the 'next' URL in the paginated response?
A"/api/items/?page=3"
B"/api/items/?page=4"
Cnull
D"/api/items/?page=2"
Attempts:
2 left
💡 Hint

Calculate total pages and which page comes after page 2.

🧠 Conceptual
expert
3:00remaining
Which pagination method is best for infinite scrolling with stable ordering?

You want to implement infinite scrolling in a Django REST Framework API. The data changes frequently, and you want to avoid skipping or repeating items when users scroll. Which pagination method is best?

ALimitOffsetPagination because it allows skipping items by offset.
BCursorPagination because it uses a cursor to track position and handles data changes well.
CPageNumberPagination because it is simple and uses page numbers.
DNo pagination; load all items at once for smooth scrolling.
Attempts:
2 left
💡 Hint

Consider how data changes affect pagination stability and user experience.