Ticket #13986: object_list_paginate_by.txt

File object_list_paginate_by.txt, 4.3 KB (added by Dougal Matthews, 14 years ago)

Added a plain text version that trac should hopefully be able to display.

Line 
1diff --git a/django/views/generic/list_detail.py b/django/views/generic/list_detail.py
2index 1e39c4d..8c9baf6 100644
3--- a/django/views/generic/list_detail.py
4+++ b/django/views/generic/list_detail.py
5@@ -45,6 +45,15 @@ def object_list(request, queryset, paginate_by=None, page=None,
6 if extra_context is None: extra_context = {}
7 queryset = queryset._clone()
8 if paginate_by:
9+
10+ try:
11+ paginate_by = int(paginate_by)
12+ if paginate_by < 1:
13+ # page sizes less than one don't make sense
14+ raise Http404
15+ except ValueError:
16+ raise Http404
17+
18 paginator = Paginator(queryset, paginate_by, allow_empty_first_page=allow_empty)
19 if not page:
20 page = request.GET.get('page', 1)
21diff --git a/tests/regressiontests/views/tests/generic/object_list.py b/tests/regressiontests/views/tests/generic/object_list.py
22index 6b1fed0..fecc37a 100644
23--- a/tests/regressiontests/views/tests/generic/object_list.py
24+++ b/tests/regressiontests/views/tests/generic/object_list.py
25@@ -17,22 +17,43 @@ class ObjectListTest(TestCase):
26 return response
27
28 def test_finds_pages(self):
29- ""
30+ """
31+ Test object_list pagination page values
32+ """
33
34- # check page count doesn't start at 0
35- response = self.check_pagination('/views/object_list/page0/', 404)
36+ base_url = '/views/object_list/paginate_by2/page%s/'
37
38- response = self.check_pagination('/views/object_list/page/', 200, )
39- response = self.check_pagination('/views/object_list/page1/', 200, 2)
40- response = self.check_pagination('/views/object_list/page2/', 200, 1)
41- response = self.check_pagination('/views/object_list/pagelast/', 200, 1)
42- response = self.check_pagination('/views/object_list/pagenotlast/', 404)
43+ # Check valid pagination requests.
44+ self.check_pagination(base_url % '', 200, )
45+ self.check_pagination(base_url % 1, 200, 2)
46+ self.check_pagination(base_url % 2, 200, 1)
47
48- response = self.check_pagination('/views/object_list/page3/', 404)
49+ # test 'last' special case
50+ self.check_pagination(base_url % 'last', 200, 1)
51
52+ # Check for page numbers higher than maximum and invalid
53+ self.check_pagination(base_url % 'notlast', 404)
54+ self.check_pagination(base_url % 3, 404)
55+ self.check_pagination(base_url % -1, 404)
56+ # check page count doesn't start at 0
57+ self.check_pagination(base_url % 0, 404)
58
59- def test_no_paginate_by(self):
60+ def test_paginate_by(self):
61+ """
62+ Test object_list paginate_by values.
63+ """
64+
65+ base_url = '/views/object_list/paginate_by%s/page1/'
66
67- response = self.check_pagination('/views/object_list_no_paginate_by/page1/', 200)
68+
69+ response = self.check_pagination(base_url % 1, 200)
70+ self.assertEqual(response.context['is_paginated'], True)
71+
72+ # Test when pagination is None
73+ response = self.check_pagination(base_url % '', 200)
74 self.assertEqual(response.context['is_paginated'], False)
75-
76\ No newline at end of file
77+
78+ # Test invalid pagination values.
79+ self.check_pagination(base_url % 'spam', 404)
80+ self.check_pagination(base_url % -1, 404)
81+ self.check_pagination(base_url % 0, 404)
82\ No newline at end of file
83diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py
84index 9cf821c..70300c4 100644
85--- a/tests/regressiontests/views/urls.py
86+++ b/tests/regressiontests/views/urls.py
87@@ -34,7 +34,6 @@ date_based_info_dict = {
88
89 object_list_dict = {
90 'queryset': Article.objects.all(),
91- 'paginate_by' : 2,
92 }
93
94 object_list_no_paginate_by = {
95@@ -116,8 +115,8 @@ urlpatterns += patterns('django.views.generic.create_update',
96 )
97
98 urlpatterns += patterns('django.views.generic.list_detail',
99- (r'^object_list/page(?P<page>[\w]*)/$', 'object_list', object_list_dict),
100- (r'^object_list_no_paginate_by/page(?P<page>[0-9]+)/$', 'object_list', object_list_no_paginate_by),
101+ (r'^object_list/paginate_by(?P<paginate_by>[-\w]*)/page(?P<page>[-\w]*)/$',
102+ 'object_list', object_list_dict),
103 )
104
105 # a view that raises an exception for the debug view
Back to Top