Opened 13 years ago

Closed 12 years ago

#17159 closed New feature (fixed)

Paginator.Page() should throw an exception for invalid [next|previous]_page_number()

Reported by: mehta.apurva@… Owned by: neaf
Component: Core (Other) Version: 1.3
Severity: Normal Keywords: paginator core
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Julien Phalip)

The next_page_number() and previous_page_number() currently return an incremented or decremented number without verifying if the page is in the range or not. They should instead throw an exception.

Repro Steps
=======

>>> from django.core.paginator import Paginator
>>> objects = ['john', 'paul', 'george', 'ringo', 'bill', 'gates', 'steve', 'jobs']
>>> p = Paginator(objects, 2)
>>> p.count
8
>>> p.num_pages
4
>>> p.page_range
[1, 2, 3, 4]
>>> p.page(1).has_previous()
False
>>> p.page(4).has_next()
False
>>> p.page(1).previous_page_number()  ### Should throw Exception
0   
>>> p.page(4).next_page_number() ### Should throw Exception
5

Attachments (2)

ticket17159.diff (576 bytes ) - added by Apurva Mehta 12 years ago.
Patch for the issue
ticket17159-v2.diff (3.9 KB ) - added by neaf 12 years ago.
Previous patch with tests and documentation update.

Download all attachments as: .zip

Change History (14)

comment:1 by anonymous, 13 years ago

Owner: changed from nobody to mehta.apurva@…

comment:2 by Julien Phalip, 12 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted

That makes sense. It would be a matter of calling Page.paginator.validate_number().

comment:3 by Julien Phalip, 12 years ago

Type: BugNew feature

by Apurva Mehta, 12 years ago

Attachment: ticket17159.diff added

Patch for the issue

comment:4 by Apurva Mehta, 12 years ago

Has patch: set
Resolution: fixed
Status: newclosed

Updated the functions to call Page.paginator.validate_number()

comment:5 by Aymeric Augustin, 12 years ago

Resolution: fixed
Status: closedreopened

Please don't mark tickets as fixed until the fix is actually committed to the code base.

comment:6 by Julien Phalip, 12 years ago

Needs documentation: set
Needs tests: set

Thank you. This would now need some tests and documentation (https://docs.djangoproject.com/en/dev/topics/pagination/).

comment:7 by neaf, 12 years ago

Owner: changed from mehta.apurva@… to neaf
Status: reopenednew

by neaf, 12 years ago

Attachment: ticket17159-v2.diff added

Previous patch with tests and documentation update.

comment:8 by neaf, 12 years ago

Attached another patch with tests and documentation.

I'm not sure if it isn't compatibility breaking change. I'm pretty sure no-one wants to display 0 and out-of-range pages but I don't know if we should throw exceptions where we haven't in the ast.

comment:9 by neaf, 12 years ago

Status: newassigned

comment:10 by Jannis Leidel, 12 years ago

Triage Stage: AcceptedReady for checkin

comment:11 by Julien Phalip, 12 years ago

Quick note: This is slightly backward-compatible and some release notes should be added.

comment:12 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In [fc40a6504b78e604a6ba90a3b85b1ed54ee238a2]:

Fixed #17159 -- Validated returned number of next|previous_page_number

Thanks mehta.apurva at gmail.com for the report and the initial patch
and neaf for the complete patch.

Note: See TracTickets for help on using tickets.
Back to Top