From 0ae3323bd237b70d62873fb052cf5617fc446229 Mon Sep 17 00:00:00 2001 From: Shi Pengtao Date: Tue, 22 Nov 2022 14:37:31 +0800 Subject: [PATCH] fix 404 when page query parameter is empty string (#8578) * fix 404 when page query parameter is empty string * Update pagination.py * Update pagination.py Co-authored-by: Tom Christie --- rest_framework/pagination.py | 2 +- tests/test_pagination.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 50e5aaacb..f5c5b913b 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -218,7 +218,7 @@ class PageNumberPagination(BasePagination): return list(self.page) def get_page_number(self, request, paginator): - page_number = request.query_params.get(self.page_query_param, 1) + page_number = request.query_params.get(self.page_query_param) or 1 if page_number in self.last_page_strings: page_number = paginator.num_pages return page_number diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 74a65bf50..2812c4489 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -180,8 +180,9 @@ class TestPageNumberPagination: def get_html_context(self): return self.pagination.get_html_context() - def test_no_page_number(self): - request = Request(factory.get('/')) + @pytest.mark.parametrize('url', ['/', '/?page=']) + def test_no_page_number(self, url): + request = Request(factory.get(url)) queryset = self.paginate_queryset(request) content = self.get_paginated_content(queryset) context = self.get_html_context()