Opened 14 years ago

Closed 14 years ago

#13689 closed Bug (fixed)

Paginator.page fails when Paginator is set up with a unicode string in the per_page parameter

Reported by: rbanffy Owned by: nobody
Component: Core (Other) Version: dev
Severity: Normal Keywords: Paginator
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The django.core.paginator.Paginator init method accepts a non-numeric value in the per_page parameter, but fails with a somewhat confusing message ("TypeError: coercing to Unicode: need string or buffer, int found") when its page method is invoked.

Here is an example of what happens:

>>> from dummy.models import *
>>> for i in range(100):
...     d = Dummy(name = 'dummy number %s' % i)
...     d.save()
... 
>>> from django.core.paginator import Paginator
>>> p = Paginator(Dummy.objects.all(), 10)
>>> p.page(1)
<Page 1 of 20>
>>> p.page(1).object_list
[<Dummy: Dummy object>, <Dummy: Dummy object>, <Dummy: Dummy object>, <Dummy: Dummy object>, <Dummy: Dummy object>, 
<Dummy: Dummy object>, <Dummy: Dummy object>, <Dummy: Dummy object>, <Dummy: Dummy object>, <Dummy: Dummy object>]
>>> p = Paginator(Dummy.objects.all(), u'10')
>>> p.page(1).object_list
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/django/core/paginator.py", line 40, in page
    if top + self.orphans >= self.count:
TypeError: coercing to Unicode: need string or buffer, int found

(a more pleasant version can be found here: http://dpaste.com/202369/)

I have quickly assembled a (ludicrously) simple patch that solves the problem and doesn't appear to break any test.

Index: django/core/paginator.py
===================================================================
--- django/core/paginator.py	(revision 13316)
+++ django/core/paginator.py	(working copy)
@@ -12,7 +12,7 @@
 class Paginator(object):
     def __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True):
         self.object_list = object_list
-        self.per_page = per_page
+        self.per_page = int(per_page)
         self.orphans = orphans
         self.allow_empty_first_page = allow_empty_first_page
         self._num_pages = self._count = None

Attachments (2)

paginator_patch_13316.diff (575 bytes ) - added by rbanffy 14 years ago.
The mentioned patch
13689.diff (1.4 KB ) - added by Claude Paroz 14 years ago.
Attaching Eric's patch as of comment #2

Download all attachments as: .zip

Change History (7)

by rbanffy, 14 years ago

Attachment: paginator_patch_13316.diff added

The mentioned patch

comment:1 by Russell Keith-Magee, 14 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 by floguy, 14 years ago

Needs tests: unset

I've updated the patch to include tests for this, and to account for string or unicode input in either the per_page or the orphans parameters. It's all isolated in a branch here: http://github.com/ericflo/django/compare/ticket13689

by Claude Paroz, 14 years ago

Attachment: 13689.diff added

Attaching Eric's patch as of comment #2

comment:3 by Claude Paroz, 14 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Julien Phalip, 14 years ago

Severity: Normal
Type: Bug

comment:5 by Jannis Leidel, 14 years ago

Resolution: fixed
Status: newclosed

In [16073]:

Fixed #13689 -- Convert the per_page value to an integer upon initialization of the Paginator class to prevent unpleasant TypeErrors. Thanks, rbanffy, Eric Florenzano and Claude Paroz.

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