loader,
)
-try:
- 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):
self.template = template
+ self.window = window
+ self.margin = margin
def render(self, context):
template_list = ['pagination/pagination.html']
- new_context = paginate(context)
+ new_context = paginate(context, window=self.window, margin=self.margin)
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
+ 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
+ else:
+ raise TemplateSyntaxError(
+ "Invalid syntax. Proper usage of this tag is: "
+ "{% paginate [using \"TEMPLATE\"] %}")
+ return PaginateNode(template, window, margin)
def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN):
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: