Ticket #14773: listview-custom-paginator.diff

File listview-custom-paginator.diff, 4.0 KB (added by piquadrat, 4 years ago)
  • django/views/generic/list.py

    diff --git a/django/views/generic/list.py b/django/views/generic/list.py
    index ef90d75..eaa06a8 100644
    a b class MultipleObjectMixin(object): 
    1010    model = None
    1111    paginate_by = None
    1212    context_object_name = None
     13    paginator_class = Paginator
    1314
    1415    def get_queryset(self):
    1516        """
    class MultipleObjectMixin(object): 
    3233        Paginate the queryset, if needed.
    3334        """
    3435        if queryset.count() > page_size:
    35             paginator = Paginator(queryset, page_size, allow_empty_first_page=self.get_allow_empty())
     36            paginator = self.paginator_class(queryset, page_size, allow_empty_first_page=self.get_allow_empty())
    3637            page = self.kwargs.get('page', None) or self.request.GET.get('page', 1)
    3738            try:
    3839                page_number = int(page)
  • docs/ref/class-based-views.txt

    diff --git a/docs/ref/class-based-views.txt b/docs/ref/class-based-views.txt
    index ada25a6..9609b76 100644
    a b MultipleObjectMixin 
    305305        expect either a ``page`` query string parameter (via ``GET``) or a
    306306        ``page`` variable specified in the URLconf.
    307307
     308    .. attribute:: paginator_class
     309
     310       The paginator class to be used for pagination. By default,
     311       :class:`django.core.paginator.Paginator` is used. Custom paginator classes have to conform to
     312       the interface defined by that class.
     313
    308314    .. attribute:: context_object_name
    309315
    310316        Designates the name of the variable to use in the context.
  • tests/regressiontests/generic_views/list.py

    diff --git a/tests/regressiontests/generic_views/list.py b/tests/regressiontests/generic_views/list.py
    index 8f6af74..622fc06 100644
    a b from django.core.exceptions import ImproperlyConfigured 
    22from django.test import TestCase
    33
    44from regressiontests.generic_views.models import Author
    5 
     5from regressiontests.generic_views.views import CustomPaginator
    66
    77class ListViewTests(TestCase):
    88    fixtures = ['generic-views-test-data.json']
    class ListViewTests(TestCase): 
    8585        self._make_authors(100)
    8686        res = self.client.get('/list/authors/paginated/?page=frog')
    8787        self.assertEqual(res.status_code, 404)
     88       
     89    def test_paginated_custom_paginator(self):
     90        self._make_authors(10)
     91        res = self.client.get('/list/authors/paginated/custom/')
     92        self.assertIs(res.context['paginator'].__class__, CustomPaginator)
    8893
    8994    def test_allow_empty_false(self):
    9095        res = self.client.get('/list/authors/notempty/')
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index 61f0387..8ed9776 100644
    a b urlpatterns = patterns('', 
    113113        views.AuthorList.as_view(context_object_name='object_list')),
    114114    (r'^list/authors/invalid/$',
    115115        views.AuthorList.as_view(queryset=None)),
     116    (r'^list/authors/paginated/custom/$',
     117        views.AuthorListCustomPaginator.as_view()),
    116118
    117119    # YearArchiveView
    118120    # Mixing keyword and possitional captures below is intentional; the views
  • tests/regressiontests/generic_views/views.py

    diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
    index eacfb71..6d63496 100644
    a b  
    11from django.contrib.auth.decorators import login_required
     2from django.core.paginator import Paginator
    23from django.core.urlresolvers import reverse
    34from django.utils.decorators import method_decorator
    45from django.views import generic
    class AuthorList(generic.ListView): 
    5051    queryset = Author.objects.all()
    5152
    5253
     54class CustomPaginator(Paginator): pass
     55
     56class AuthorListCustomPaginator(AuthorList):
     57    paginator_class = CustomPaginator
     58    paginate_by = 5
     59
    5360
    5461class ArtistCreate(generic.CreateView):
    5562    model = Artist
Back to Top