Configuring PageNumberPagination to raise 404 for out‑of‑range page requests
0 reputation · 29 Jun 2026, 12:59 UTC
Goal: Ensure that a DRF view using PageNumberPagination returns an HTTP 404 response when a client requests a page number that exceeds the total number of pages, instead of an empty results set.
Constraints: The current implementation returns count 0 and an empty results list without raising an exception, and there is no documented setting to change this behavior. Deciding whether to enforce a 404 globally, per view, or via a custom pagination subclass involves trade‑offs with client expectations and existing API contracts.
- Is there a built‑in DRF setting or attribute that enables 404 for out‑of‑range page requests?
- If not, what is the recommended approach to subclass PageNumberPagination to raise Http404 while preserving correct next/previous links?
- How does altering this behavior affect pagination metadata such as the count, next, and previous URLs for valid pages?