Skip to content

Commit f84e743

Browse files
committed
Prevent raising exception when limit is 0
1 parent 399e1c1 commit f84e743

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

rest_framework/pagination.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ def _positive_int(integer_string, strict=False, cutoff=None):
3636

3737
def _divide_with_ceil(a, b):
3838
"""
39-
Returns 'a' divded by 'b', with any remainder rounded up.
39+
Returns 'a' divided by 'b', with any remainder rounded up.
4040
"""
4141
if a % b:
4242
return (a // b) + 1
43+
4344
return a // b
4445

4546

@@ -358,17 +359,24 @@ def get_previous_link(self):
358359

359360
def get_html_context(self):
360361
base_url = self.request.build_absolute_uri()
361-
current = _divide_with_ceil(self.offset, self.limit) + 1
362-
# The number of pages is a little bit fiddly.
363-
# We need to sum both the number of pages from current offset to end
364-
# plus the number of pages up to the current offset.
365-
# When offset is not strictly divisible by the limit then we may
366-
# end up introducing an extra page as an artifact.
367-
final = (
368-
_divide_with_ceil(self.count - self.offset, self.limit) +
369-
_divide_with_ceil(self.offset, self.limit)
370-
)
371-
if final < 1:
362+
363+
if self.limit:
364+
current = _divide_with_ceil(self.offset, self.limit) + 1
365+
366+
# The number of pages is a little bit fiddly.
367+
# We need to sum both the number of pages from current offset to end
368+
# plus the number of pages up to the current offset.
369+
# When offset is not strictly divisible by the limit then we may
370+
# end up introducing an extra page as an artifact.
371+
final = (
372+
_divide_with_ceil(self.count - self.offset, self.limit) +
373+
_divide_with_ceil(self.offset, self.limit)
374+
)
375+
376+
if final < 1:
377+
final = 1
378+
else:
379+
current = 1
372380
final = 1
373381

374382
if current > final:

tests/test_pagination.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,31 @@ def test_max_limit(self):
505505
assert content.get('next') == next_url
506506
assert content.get('previous') == prev_url
507507

508+
def test_limit_zero(self):
509+
"""
510+
A limit of 0 should return empty results.
511+
"""
512+
request = Request(factory.get('/', {'limit': 0, 'offset': 10}))
513+
queryset = self.paginate_queryset(request)
514+
context = self.get_html_context()
515+
content = self.get_paginated_content(queryset)
516+
517+
assert context == {
518+
'previous_url': 'http://testserver/?limit=0&offset=10',
519+
'page_links': [
520+
PageLink(
521+
url='http://testserver/?limit=0',
522+
number=1,
523+
is_active=True,
524+
is_break=False
525+
)
526+
],
527+
'next_url': 'http://testserver/?limit=0&offset=10'
528+
}
529+
530+
assert queryset == []
531+
assert content.get('results') == []
532+
508533

509534
class TestCursorPagination:
510535
"""

0 commit comments

Comments
 (0)