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 <tom@tomchristie.com>
This commit is contained in:
Shi Pengtao 2022-11-22 14:37:31 +08:00 committed by GitHub
parent 03c2ef1787
commit 0ae3323bd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 3 deletions

View File

@ -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

View File

@ -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()