loader,
)
-try:
- from django.template.base import TokenType
- TOKEN_BLOCK = TokenType.BLOCK
-except ImportError:
- try:
- # Django 1.8-1.11
- from django.template.base import TOKEN_BLOCK
- except ImportError: # Django < 1.8
- from django.template import TOKEN_BLOCK
+from django.template.base import TokenType
+TOKEN_BLOCK = TokenType.BLOCK
from django.template.loader import select_template
from django.utils.text import unescape_string_literal
if self.multiple_paginations and 'multiple_paginations' not in context:
context['multiple_paginations'] = True
- if context.get('multiple_paginations') or getattr(context, "paginator", None):
+ if context.get('page_suffix'):
+ page_suffix = context['page_suffix']
+ elif context.get('multiple_paginations') or getattr(context, "paginator", None):
page_suffix = '_%s' % self.queryset_var
else:
page_suffix = ''
class PaginateNode(Node):
- def __init__(self, template=None):
+ def __init__(self, template=None, window=None, margin=None, ignored_vars=None):
self.template = template
+ self.window = window
+ self.margin = margin
+ self.ignored_vars = ignored_vars
def render(self, context):
template_list = ['pagination/pagination.html']
- new_context = paginate(context)
+ new_context = paginate(context, window=self.window, margin=self.margin, ignored_vars=self.ignored_vars)
if self.template:
template_list.insert(0, self.template)
return loader.render_to_string(template_list, new_context)
Syntax is:
- paginate [using "TEMPLATE"]
+ paginate [using "TEMPLATE"] [window N] [margin N]
Where TEMPLATE is a quoted template name. If missing the default template
is used (paginate/pagination.html).
"""
argv = token.split_contents()
argc = len(argv)
- if argc == 1:
- template = None
- elif argc == 3 and argv[1] == 'using':
- template = unescape_string_literal(argv[2])
- else:
- raise TemplateSyntaxError(
- "Invalid syntax. Proper usage of this tag is: "
- "{% paginate [using \"TEMPLATE\"] %}")
- return PaginateNode(template)
+ template = None
+ window = DEFAULT_WINDOW
+ margin = DEFAULT_MARGIN
+ ignored_vars = []
+ i = 1
+ while i < argc:
+ if argv[i] == 'using':
+ template = unescape_string_literal(argv[i + 1])
+ i += 2
+ elif argv[i] == 'window':
+ window = argv[i + 1]
+ i += 2
+ elif argv[i] == 'margin':
+ margin = argv[i + 1]
+ i += 2
+ elif argv[i] == 'ignore':
+ ignored_vars.append(argv[i + 1])
+ i += 2
+ else:
+ raise TemplateSyntaxError(
+ "Invalid syntax. Proper usage of this tag is: "
+ "{% paginate [using \"TEMPLATE\"] %}")
+ return PaginateNode(template, window, margin, ignored_vars)
-def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN):
+def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN, ignored_vars=None):
"""
Renders the ``pagination/pagination.html`` template, resulting in a
Digg-like display of the available pages, given the current page. If there
window=2, margin=0, current=11 ... 7 8 9 10 [11]
"""
+ try:
+ window = int(window)
+ except ValueError:
+ window = Variable(window).resolve(context)
+ try:
+ margin = int(margin)
+ except ValueError:
+ margin = Variable(margin).resolve(context)
+
if window < 0:
raise ValueError('Parameter "window" cannot be less than zero')
if margin < 0:
}
if 'request' in context:
getvars = context['request'].GET.copy()
+ if ignored_vars:
+ for v in ignored_vars:
+ if v in getvars:
+ del getvars[v]
if 'page%s' % page_suffix in getvars:
del getvars['page%s' % page_suffix]
if len(getvars.keys()) > 0: