diff --git a/django/views/generic/list_detail.py b/django/views/generic/list_detail.py index 1e39c4d..8c9baf6 100644 --- a/django/views/generic/list_detail.py +++ b/django/views/generic/list_detail.py @@ -45,6 +45,15 @@ def object_list(request, queryset, paginate_by=None, page=None, if extra_context is None: extra_context = {} queryset = queryset._clone() if paginate_by: + + try: + paginate_by = int(paginate_by) + if paginate_by < 1: + # page sizes less than one don't make sense + raise Http404 + except ValueError: + raise Http404 + paginator = Paginator(queryset, paginate_by, allow_empty_first_page=allow_empty) if not page: 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/tests/regressiontests/views/tests/generic/object_list.py +++ b/tests/regressiontests/views/tests/generic/object_list.py @@ -17,22 +17,43 @@ class ObjectListTest(TestCase): return response def test_finds_pages(self): - "" + """ + Test object_list pagination page values + """ - # check page count doesn't start at 0 - response = self.check_pagination('/views/object_list/page0/', 404) + base_url = '/views/object_list/paginate_by2/page%s/' - response = self.check_pagination('/views/object_list/page/', 200, ) - response = self.check_pagination('/views/object_list/page1/', 200, 2) - response = self.check_pagination('/views/object_list/page2/', 200, 1) - response = self.check_pagination('/views/object_list/pagelast/', 200, 1) - response = self.check_pagination('/views/object_list/pagenotlast/', 404) + # Check valid pagination requests. + self.check_pagination(base_url % '', 200, ) + self.check_pagination(base_url % 1, 200, 2) + self.check_pagination(base_url % 2, 200, 1) - response = self.check_pagination('/views/object_list/page3/', 404) + # test 'last' special case + self.check_pagination(base_url % 'last', 200, 1) + # Check for page numbers higher than maximum and invalid + self.check_pagination(base_url % 'notlast', 404) + self.check_pagination(base_url % 3, 404) + self.check_pagination(base_url % -1, 404) + # check page count doesn't start at 0 + self.check_pagination(base_url % 0, 404) - def test_no_paginate_by(self): + def test_paginate_by(self): + """ + Test object_list paginate_by values. + """ + + base_url = '/views/object_list/paginate_by%s/page1/' - response = self.check_pagination('/views/object_list_no_paginate_by/page1/', 200) + + response = self.check_pagination(base_url % 1, 200) + self.assertEqual(response.context['is_paginated'], True) + + # Test when pagination is None + response = self.check_pagination(base_url % '', 200) self.assertEqual(response.context['is_paginated'], False) - \ No newline at end of file + + # Test invalid pagination values. + self.check_pagination(base_url % 'spam', 404) + self.check_pagination(base_url % -1, 404) + self.check_pagination(base_url % 0, 404) \ 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/tests/regressiontests/views/urls.py +++ b/tests/regressiontests/views/urls.py @@ -34,7 +34,6 @@ date_based_info_dict = { object_list_dict = { 'queryset': Article.objects.all(), - 'paginate_by' : 2, } object_list_no_paginate_by = { @@ -116,8 +115,8 @@ urlpatterns += patterns('django.views.generic.create_update', ) urlpatterns += patterns('django.views.generic.list_detail', - (r'^object_list/page(?P[\w]*)/$', 'object_list', object_list_dict), - (r'^object_list_no_paginate_by/page(?P[0-9]+)/$', 'object_list', object_list_no_paginate_by), + (r'^object_list/paginate_by(?P[-\w]*)/page(?P[-\w]*)/$', + 'object_list', object_list_dict), ) # a view that raises an exception for the debug view