diff --git a/django/core/paginator.py b/django/core/paginator.py
index 495cdf2..479cb49 100644
a
|
b
|
|
1 | 1 | from math import ceil |
| 2 | import collections |
2 | 3 | |
3 | 4 | class InvalidPage(Exception): |
4 | 5 | pass |
… |
… |
class Paginator(object):
|
75 | 76 | |
76 | 77 | QuerySetPaginator = Paginator # For backwards-compatibility. |
77 | 78 | |
78 | | class Page(object): |
| 79 | class Page(collections.Sequence): |
79 | 80 | def __init__(self, object_list, number, paginator): |
80 | 81 | self.object_list = object_list |
81 | 82 | self.number = number |
… |
… |
class Page(object):
|
84 | 85 | def __repr__(self): |
85 | 86 | return '<Page %s of %s>' % (self.number, self.paginator.num_pages) |
86 | 87 | |
| 88 | def __len__(self): |
| 89 | return len(self.object_list) |
| 90 | |
| 91 | def __getitem__(self, index): |
| 92 | # The object_list is converted to a list so that if it was a QuerySet |
| 93 | # it won't be a database hit per __getitem__. |
| 94 | return list(self.object_list)[index] |
| 95 | |
87 | 96 | def has_next(self): |
88 | 97 | return self.number < self.paginator.num_pages |
89 | 98 | |
diff --git a/tests/regressiontests/pagination_regress/tests.py b/tests/regressiontests/pagination_regress/tests.py
index 28fe316..1cddd43 100644
a
|
b
|
class PaginatorTests(TestCase):
|
154 | 154 | self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 0, False), 1, None) |
155 | 155 | self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 1, False), 1, None) |
156 | 156 | self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 2, False), 1, None) |
| 157 | |
| 158 | def test_page_sequence(self): |
| 159 | """ |
| 160 | Tests that a paginator page acts like a standard sequence. |
| 161 | """ |
| 162 | eleven = 'abcdefghijk' |
| 163 | page2 = Paginator(eleven, per_page=5, orphans=1).page(2) |
| 164 | self.assertEqual(len(page2), 6) |
| 165 | self.assertTrue('k' in page2) |
| 166 | self.assertFalse('a' in page2) |
| 167 | self.assertEqual(''.join(page2), 'fghijk') |