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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.