Login

BetterQuerySetPaginator

Author:
zeeg
Posted:
July 10, 2008
Language:
Python
Version:
.96
Tags:
pagination paginator
Score:
2 (after 2 ratings)

A more useful paginator.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django.core.paginator import QuerySetPaginator, InvalidPage

__all__ = ('BetterQuerySetPaginator', 'InvalidPage')

class BetterQuerySetPaginator(QuerySetPaginator):
    """
    An enhanced version of the QuerySetPaginator.
    
    >>> my_objects = BetterQuerySetPaginator(queryset, 25)
    >>> page = 1
    >>> context = {
    >>>     'my_objects': my_objects.get_context(page),
    >>> }
    """
    def get_context(self, page, range_gap=5):
        try:
            page = int(page)
        except (ValueError, TypeError), exc:
            raise InvalidPage, exc
        
        if page > 5:
            start = page-range_gap
        else:
            start = 1

        if page < self.num_pages-range_gap:
            end = page+range_gap+1
        else:
            end = self.num_pages+1

        paginator = self.page(page)

        context = {
            'page_range': range(start, end),
            'objects': paginator.object_list,
            'num_pages': self.num_pages,
            'page': page,
            'has_previous': paginator.has_previous(),
            'has_next': paginator.has_next(),
            'previous_page': paginator.previous_page_number(),
            'next_page': paginator.next_page_number(),
            'is_first': page == 1,
            'is_last': page == self.num_pages,
        }
        
        return context

More like this

  1. BetterPaginator by spankalee 7 years, 12 months ago
  2. deleted by hassa 2 years ago
  3. Pretty Paginator Tag by sbaechler 3 years, 2 months ago
  4. BetterPaginator Template Code by spankalee 7 years, 12 months ago
  5. django-pagination with "clean url" by pafa 3 years, 7 months ago

Comments

Please login first before commenting.