Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which Django REST Framework pagination style uses a page number to fetch specific pages of data?
easy
A. PageNumberPagination
B. CursorPagination
C. LimitOffsetPagination
D. OffsetPagination

Solution

  1. Step 1: Understand pagination styles

    PageNumberPagination uses page numbers like 1, 2, 3 to get data pages.
  2. Step 2: Match style to description

    CursorPagination uses a cursor token, LimitOffsetPagination uses limit and offset numbers, so they don't use page numbers.
  3. Final Answer:

    PageNumberPagination -> Option A
  4. Quick Check:

    Page number style = PageNumberPagination [OK]
Hint: PageNumberPagination uses simple page numbers like 1, 2, 3 [OK]
Common Mistakes:
  • Confusing CursorPagination with page numbers
  • Thinking LimitOffsetPagination uses page numbers
  • Assuming OffsetPagination is a valid DRF style
2. Which of the following is the correct way to set LimitOffsetPagination in Django REST Framework settings?
easy
A. "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.OffsetPagination"
B. "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination"
C. "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination"
D. "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.CursorPagination"

Solution

  1. Step 1: Identify correct class path

    LimitOffsetPagination is located at rest_framework.pagination.LimitOffsetPagination.
  2. Step 2: Verify syntax for settings

    The setting key is DEFAULT_PAGINATION_CLASS and the value is the full class path as a string.
  3. Final Answer:

    "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination" -> Option C
  4. Quick Check:

    Correct class path and setting key = "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination" [OK]
Hint: Use full class path string for pagination in settings [OK]
Common Mistakes:
  • Using OffsetPagination which does not exist
  • Missing quotes around class path string
  • Mixing pagination class names
3. Given this code snippet using CursorPagination, what will be the value of the next cursor if the current cursor is 'abc123' and page size is 2?
class MyCursorPagination(CursorPagination):
    page_size = 2

paginator = MyCursorPagination()
next_cursor = paginator.get_next_link()
medium
A. A tuple with offset and limit values
B. An integer representing the next page number
C. None, because get_next_link() returns nothing
D. A URL containing a cursor parameter with a new encoded cursor

Solution

  1. Step 1: Understand CursorPagination behavior

    CursorPagination returns URLs with encoded cursor tokens for next pages, not page numbers or tuples.
  2. Step 2: Analyze get_next_link() output

    get_next_link() returns a URL string containing the next cursor parameter for pagination.
  3. Final Answer:

    A URL containing a cursor parameter with a new encoded cursor -> Option D
  4. Quick Check:

    CursorPagination next link = URL with cursor [OK]
Hint: CursorPagination returns URLs with cursor tokens, not numbers [OK]
Common Mistakes:
  • Expecting page numbers from CursorPagination
  • Thinking get_next_link() returns None
  • Confusing limit/offset with cursor
4. You have this Django REST Framework view using LimitOffsetPagination but it raises an error:
class MyLimitOffsetPagination(LimitOffsetPagination):
    default_limit = '10'

class MyView(ListAPIView):
    pagination_class = MyLimitOffsetPagination
    queryset = MyModel.objects.all()
    serializer_class = MySerializer

What is the likely cause of the error?
medium
A. pagination_class should be a string path, not a class
B. default_limit should be an integer, not a string
C. ListAPIView does not support pagination
D. queryset must be a list, not a QuerySet

Solution

  1. Step 1: Check default_limit type

    default_limit must be an integer, but it is set as a string '10', causing a type error.
  2. Step 2: Verify other parts

    pagination_class can be a class, ListAPIView supports pagination, queryset can be a QuerySet.
  3. Final Answer:

    default_limit should be an integer, not a string -> Option B
  4. Quick Check:

    default_limit type error = default_limit should be an integer, not a string [OK]
Hint: default_limit must be int, not quoted string [OK]
Common Mistakes:
  • Setting default_limit as string instead of int
  • Thinking pagination_class must be string path
  • Assuming ListAPIView disables pagination
5. You want to implement pagination for a large dataset where new items are frequently added. Which pagination style is best to avoid duplicate or missing items when users navigate pages?
hard
A. CursorPagination
B. PageNumberPagination
C. LimitOffsetPagination
D. No pagination

Solution

  1. Step 1: Understand pagination challenges with dynamic data

    PageNumber and LimitOffset can cause duplicates or missing items if data changes between requests.
  2. Step 2: Identify pagination style that handles dynamic data well

    CursorPagination uses a stable cursor based on item order, preventing duplicates or skips when data changes.
  3. Final Answer:

    CursorPagination -> Option A
  4. Quick Check:

    Dynamic data needs CursorPagination [OK]
Hint: Use CursorPagination for changing data to avoid duplicates [OK]
Common Mistakes:
  • Choosing PageNumberPagination for dynamic data
  • Thinking LimitOffsetPagination handles data changes well
  • Ignoring pagination for large datasets