From 3922b9738030492579d35ac5f354c07d6833c0a7 Mon Sep 17 00:00:00 2001 From: Paul Wayper Date: Fri, 6 Nov 2020 08:31:29 +1100 Subject: [PATCH] Invalid page numbers get treated as '1' Signed-off-by: Paul Wayper --- rest_framework/pagination.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 9578eada9..0d18885f2 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -265,8 +265,12 @@ class PageNumberPagination(BasePagination): return self.page_size def get_page_number(self, request): - # This can be negative to mean 'pages from the end'. - return int(request.query_params.get(self.page_query_param, 1)) + # This can be negative to mean 'pages from the end'. Invalid values + # are ignored, and the default is page 1. + try: + return int(request.query_params.get(self.page_query_param, 1)) + except ValueError: + return 1 def get_next_link(self): if not self.page.has_next():