diff --git a/django/views/generic/list_detail.py b/django/views/generic/list_detail.py
index 1e39c4d..8c9baf6 100644
a
|
b
|
def object_list(request, queryset, paginate_by=None, page=None,
|
45 | 45 | if extra_context is None: extra_context = {} |
46 | 46 | queryset = queryset._clone() |
47 | 47 | if paginate_by: |
| 48 | |
| 49 | try: |
| 50 | paginate_by = int(paginate_by) |
| 51 | if paginate_by < 1: |
| 52 | # page sizes less than one don't make sense |
| 53 | raise Http404 |
| 54 | except ValueError: |
| 55 | raise Http404 |
| 56 | |
48 | 57 | paginator = Paginator(queryset, paginate_by, allow_empty_first_page=allow_empty) |
49 | 58 | if not page: |
50 | 59 | page = request.GET.get('page', 1) |
diff --git a/tests/regressiontests/views/tests/generic/object_list.py b/tests/regressiontests/views/tests/generic/object_list.py
index 6b1fed0..fecc37a 100644
a
|
b
|
class ObjectListTest(TestCase):
|
17 | 17 | return response |
18 | 18 | |
19 | 19 | def test_finds_pages(self): |
20 | | "" |
| 20 | """ |
| 21 | Test object_list pagination page values |
| 22 | """ |
21 | 23 | |
22 | | # check page count doesn't start at 0 |
23 | | response = self.check_pagination('/views/object_list/page0/', 404) |
| 24 | base_url = '/views/object_list/paginate_by2/page%s/' |
24 | 25 | |
25 | | response = self.check_pagination('/views/object_list/page/', 200, ) |
26 | | response = self.check_pagination('/views/object_list/page1/', 200, 2) |
27 | | response = self.check_pagination('/views/object_list/page2/', 200, 1) |
28 | | response = self.check_pagination('/views/object_list/pagelast/', 200, 1) |
29 | | response = self.check_pagination('/views/object_list/pagenotlast/', 404) |
| 26 | # Check valid pagination requests. |
| 27 | self.check_pagination(base_url % '', 200, ) |
| 28 | self.check_pagination(base_url % 1, 200, 2) |
| 29 | self.check_pagination(base_url % 2, 200, 1) |
30 | 30 | |
31 | | response = self.check_pagination('/views/object_list/page3/', 404) |
| 31 | # test 'last' special case |
| 32 | self.check_pagination(base_url % 'last', 200, 1) |
32 | 33 | |
| 34 | # Check for page numbers higher than maximum and invalid |
| 35 | self.check_pagination(base_url % 'notlast', 404) |
| 36 | self.check_pagination(base_url % 3, 404) |
| 37 | self.check_pagination(base_url % -1, 404) |
| 38 | # check page count doesn't start at 0 |
| 39 | self.check_pagination(base_url % 0, 404) |
33 | 40 | |
34 | | def test_no_paginate_by(self): |
| 41 | def test_paginate_by(self): |
| 42 | """ |
| 43 | Test object_list paginate_by values. |
| 44 | """ |
| 45 | |
| 46 | base_url = '/views/object_list/paginate_by%s/page1/' |
35 | 47 | |
36 | | response = self.check_pagination('/views/object_list_no_paginate_by/page1/', 200) |
| 48 | |
| 49 | response = self.check_pagination(base_url % 1, 200) |
| 50 | self.assertEqual(response.context['is_paginated'], True) |
| 51 | |
| 52 | # Test when pagination is None |
| 53 | response = self.check_pagination(base_url % '', 200) |
37 | 54 | self.assertEqual(response.context['is_paginated'], False) |
38 | | |
39 | | No newline at end of file |
| 55 | |
| 56 | # Test invalid pagination values. |
| 57 | self.check_pagination(base_url % 'spam', 404) |
| 58 | self.check_pagination(base_url % -1, 404) |
| 59 | self.check_pagination(base_url % 0, 404) |
| 60 | No newline at end of file |
diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py
index 9cf821c..70300c4 100644
a
|
b
|
date_based_info_dict = {
|
34 | 34 | |
35 | 35 | object_list_dict = { |
36 | 36 | 'queryset': Article.objects.all(), |
37 | | 'paginate_by' : 2, |
38 | 37 | } |
39 | 38 | |
40 | 39 | object_list_no_paginate_by = { |
… |
… |
urlpatterns += patterns('django.views.generic.create_update',
|
116 | 115 | ) |
117 | 116 | |
118 | 117 | urlpatterns += patterns('django.views.generic.list_detail', |
119 | | (r'^object_list/page(?P<page>[\w]*)/$', 'object_list', object_list_dict), |
120 | | (r'^object_list_no_paginate_by/page(?P<page>[0-9]+)/$', 'object_list', object_list_no_paginate_by), |
| 118 | (r'^object_list/paginate_by(?P<paginate_by>[-\w]*)/page(?P<page>[-\w]*)/$', |
| 119 | 'object_list', object_list_dict), |
121 | 120 | ) |
122 | 121 | |
123 | 122 | # a view that raises an exception for the debug view |