Ticket #13986: object_list_paginate_by.diff

File object_list_paginate_by.diff, 4.3 KB (added by Dougal Matthews, 14 years ago)
  • django/views/generic/list_detail.py

    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,  
    4545    if extra_context is None: extra_context = {}
    4646    queryset = queryset._clone()
    4747    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       
    4857        paginator = Paginator(queryset, paginate_by, allow_empty_first_page=allow_empty)
    4958        if not page:
    5059            page = request.GET.get('page', 1)
  • tests/regressiontests/views/tests/generic/object_list.py

    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):  
    1717        return response
    1818       
    1919    def test_finds_pages(self):
    20         ""
     20        """
     21        Test object_list pagination page values
     22        """
    2123       
    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/'
    2425       
    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)
    3030       
    31         response = self.check_pagination('/views/object_list/page3/', 404)
     31        # test 'last' special case
     32        self.check_pagination(base_url % 'last', 200, 1)
    3233       
     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)
    3340   
    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/'
    3547       
    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)
    3754        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
  • tests/regressiontests/views/urls.py

    diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py
    index 9cf821c..70300c4 100644
    a b date_based_info_dict = {  
    3434
    3535object_list_dict = {
    3636    'queryset': Article.objects.all(),
    37     'paginate_by' : 2,
    3837}
    3938
    4039object_list_no_paginate_by = {
    urlpatterns += patterns('django.views.generic.create_update',  
    116115)
    117116
    118117urlpatterns += 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),
    121120)
    122121
    123122# a view that raises an exception for the debug view
Back to Top