Opened 5 years ago

Closed 4 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: master
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: UI/UX:

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 5 years ago.
The mentioned patch
13689.diff (1.4 KB) - added by claudep 4 years ago.
Attaching Eric's patch as of comment #2

Download all attachments as: .zip

Change History (7)

Changed 5 years ago by rbanffy

The mentioned patch

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 years ago by floguy

  • 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

Changed 4 years ago by claudep

Attaching Eric's patch as of comment #2

comment:3 Changed 4 years ago by claudep

  • Triage Stage changed from Accepted to Ready for checkin

comment:4 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 4 years ago by jezdez

  • Resolution set to fixed
  • Status changed from new to closed

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