from django.core.paginator import (Paginator, EmptyPage, PageNotAnInteger) class GenericPaginator(object): """ class PostList(GenericPaginator, ListView): .... def get_context_data(self, **kwargs): context_data = super(PostList, self).get_context_data(**kwargs) context_data['page_range'] = self.get_page_range() return context_data """ def get_page_range(self): page = self.request.GET.get('page') paginate_by = self.paginate_by or 10 queryset = self.queryset or self.get_queryset() # if page is None: # raise Exception('request `?page` is None') paginator = Paginator(queryset, paginate_by) try: objects = paginator.page(page) except PageNotAnInteger: objects = paginator.page(1) except EmptyPage: objects = paginator.page(paginator.num_pages) index = objects.number - 1 limit = 3 # limit for show range left and right of number pages max_index = len(paginator.page_range) start_index = index - limit if index >= limit else 0 end_index = index + limit if index <= max_index - limit else max_index # When you return this, you will getting error # `page_range TypeError: sequence index must be integer, not 'slice'`. # Because now in django changelog, use `xrange`, and not `range`. # See this tickets: https://code.djangoproject.com/ticket/23140 # >>> page_range = paginator.page_range[start_index:end_index] page_range = list(paginator.page_range)[start_index:end_index] return page_range # USAGE IN THE TEMPLATE """ {% if is_paginated %} {# `is_paginated` is default bassed in `generic.ListView` #}
{% endif %}{# endif is_paginated #} """