Opened 15 years ago
Closed 15 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)
Change History (7)
by , 15 years ago
| Attachment: | paginator_patch_13316.diff added |
|---|
comment:1 by , 15 years ago
| Needs tests: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 15 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
comment:3 by , 15 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:4 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Bug |
The mentioned patch