From: Daniel Mizyrycki Date: Sat, 12 Sep 2009 19:58:42 +0000 (+0800) Subject: Add records variable in templatetags, allowing to extend the pagination template... X-Git-Tag: 1.0.6~1^2~4 X-Git-Url: https://git.mdrn.pl/django-pagination.git/commitdiff_plain/f6c6ae2af328d6cabbd46120d77671c9abf20e3b?hp=--cc Add records variable in templatetags, allowing to extend the pagination template with the record range displayed in the page. --- f6c6ae2af328d6cabbd46120d77671c9abf20e3b diff --git a/pagination/templatetags/pagination_tags.py b/pagination/templatetags/pagination_tags.py index 3733434..fa87b37 100644 --- a/pagination/templatetags/pagination_tags.py +++ b/pagination/templatetags/pagination_tags.py @@ -133,6 +133,11 @@ def paginate(context, window=DEFAULT_WINDOW): paginator = context['paginator'] page_obj = context['page_obj'] page_range = paginator.page_range + # Calculate the record range in the current page for display. + records = {'first': 1 + (page_obj.number - 1) * paginator.per_page} + records['last'] = records['first'] + paginator.per_page - 1 + if records['last'] + paginator.orphans >= paginator.count: + records['last'] = paginator.count # First and last are simply the first *n* pages and the last *n* pages, # where *n* is the current window size. first = set(page_range[:window]) @@ -201,6 +206,7 @@ def paginate(context, window=DEFAULT_WINDOW): to_return = { 'MEDIA_URL': settings.MEDIA_URL, 'pages': pages, + 'records': records, 'page_obj': page_obj, 'paginator': paginator, 'is_paginated': paginator.count > paginator.per_page, diff --git a/pagination/tests.py b/pagination/tests.py index a55ef49..31b3301 100644 --- a/pagination/tests.py +++ b/pagination/tests.py @@ -4,8 +4,22 @@ >>> from django.template import Template, Context >>> p = Paginator(range(15), 2) ->>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages'] +>>> pg = paginate({'paginator': p, 'page_obj': p.page(1)}) +>>> pg['pages'] +[1, 2, 3, 4, 5, 6, 7, 8] +>>> pg['records']['first'] +1 +>>> pg['records']['last'] +2 + +>>> p = Paginator(range(15), 2) +>>> pg = paginate({'paginator': p, 'page_obj': p.page(8)}) +>>> pg['pages'] [1, 2, 3, 4, 5, 6, 7, 8] +>>> pg['records']['first'] +15 +>>> pg['records']['last'] +15 >>> p = Paginator(range(17), 2) >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages'] @@ -25,8 +39,22 @@ [1, 2] >>> p = Paginator(range(21), 2, 1) ->>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages'] +>>> pg = paginate({'paginator': p, 'page_obj': p.page(1)}) +>>> pg['pages'] [1, 2, 3, 4, None, 7, 8, 9, 10] +>>> pg['records']['first'] +1 +>>> pg['records']['last'] +2 + +>>> p = Paginator(range(21), 2, 1) +>>> pg = paginate({'paginator': p, 'page_obj': p.page(10)}) +>>> pg['pages'] +[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +>>> pg['records']['first'] +19 +>>> pg['records']['last'] +21 >>> t = Template("{% load pagination_tags %}{% autopaginate var 2 %}{% paginate %}") @@ -127,4 +155,4 @@ True >>> request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart', 'wsgi.input': StringIO()}) >>> middleware.process_request(request) >>> request.upload_handlers.append('asdf') -""" \ No newline at end of file +"""