Django

Code

Ticket #9195 (new)

Opened 1 year ago

Last modified 11 months ago

Paginator.page() should raise PageNotAnInteger when given a NoneType value

Reported by: thomas@ether.com.au Assigned to: nobody
Milestone: Component: Core framework
Version: 1.0 Keywords:
Cc: Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The documentation suggests that Paginator.page() raises PageNotAnInteger? for non-integer values, however passing it None raises a TypeError?. I would imagine this is a fairly common use case. If a URL optionally has a search string that specifies a page number (eg. ?page=3) you would want to pass request.GET.get('page') to Paginator.page. However, if the request lacks the search string, page() should logically return the PageNotAnInteger? exception.

>>> from django.core.paginator import Paginator
>>> objects = ['john', 'paul', 'george', 'ringo']
>>> p = Paginator(objects, 2)
>>> p.page(1)
<Page 1 of 2>
>>> p.page("Pete Best")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home2/ether2/webapps/migration/lib/python2.5/django/core/paginator.py", line 37, in page
    number = self.validate_number(number)
  File "/home2/ether2/webapps/migration/lib/python2.5/django/core/paginator.py", line 25, in validate_number
    raise PageNotAnInteger('That page number is not an integer')
PageNotAnInteger: That page number is not an integer
>>> p.page(None)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home2/ether2/webapps/migration/lib/python2.5/django/core/paginator.py", line 37, in page
    number = self.validate_number(number)
  File "/home2/ether2/webapps/migration/lib/python2.5/django/core/paginator.py", line 23, in validate_number
    number = int(number)
TypeError: int() argument must be a string or a number, not 'NoneType'

Attachments

Change History

09/24/08 08:45:33 changed by kmtracey

  • needs_better_patch changed.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests changed.
  • needs_docs changed.

I'm not sure this is such a common thing, I think it is more likely that programmers have protected against 'page' not being in the search string by specifying a sensible default on the get, e.g. request.GET.get('page', 1). Technically changing the exception raised would be backwards-incompatible (though I doubt anyone has written code that depends on this behavior...still, it's impossible to know for sure). I'd be inclined to adjust the doc instead of the code to address this.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

05/06/09 22:50:05 changed by thejaswi_puthraya

  • component changed from Uncategorized to Core framework.

Add/Change #9195 (Paginator.page() should raise PageNotAnInteger when given a NoneType value)




Change Properties
Action