Context,
Library,
Node,
- TOKEN_BLOCK,
TemplateSyntaxError,
Variable,
loader,
)
+
+try:
+ from django.template.base import TOKEN_BLOCK
+except ImportError: # Django < 1.8
+ from django.template import TOKEN_BLOCK
+
from django.template.loader import select_template
from django.utils.text import unescape_string_literal
# Check whether there are any other autopaginations are later in this template
expr = lambda obj: (obj.token_type == TOKEN_BLOCK and \
len(obj.split_contents()) > 0 and obj.split_contents()[0] == "autopaginate")
- multiple_paginations = len(filter(expr, parser.tokens)) > 0
+ multiple_paginations = len([tok for tok in parser.tokens if expr(tok)]) > 0
i = iter(token.split_contents())
paginate_by = None
orphans = None
word = None
try:
- word = i.next()
+ word = next(i)
assert word == "autopaginate"
- queryset_var = i.next()
- word = i.next()
+ queryset_var = next(i)
+ word = next(i)
if word != "as":
paginate_by = word
try:
paginate_by = int(paginate_by)
except ValueError:
pass
- word = i.next()
+ word = next(i)
if word != "as":
orphans = word
try:
orphans = int(orphans)
except ValueError:
pass
- word = i.next()
+ word = next(i)
assert word == "as"
- context_var = i.next()
+ context_var = next(i)
except StopIteration:
pass
if queryset_var is None:
self.multiple_paginations = multiple_paginations
def render(self, context):
- if self.multiple_paginations or getattr(context, "paginator", None):
+ # Save multiple_paginations state in context
+ if self.multiple_paginations and 'multiple_paginations' not in context:
+ context['multiple_paginations'] = True
+
+ if context.get('multiple_paginations') or getattr(context, "paginator", None):
page_suffix = '_%s' % self.queryset_var
else:
page_suffix = ''
'False, an HTTP 404 page would have been shown instead.')
context[key] = []
context['invalid_page'] = True
- return u''
+ return ''
if self.context_var is not None:
context[self.context_var] = page_obj.object_list
else:
context['paginator'] = paginator
context['page_obj'] = page_obj
context['page_suffix'] = page_suffix
- return u''
+ return ''
class PaginateNode(Node):
new_context = paginate(context)
if self.template:
template_list.insert(0, self.template)
- return loader.render_to_string(template_list, new_context,
+ return loader.render_to_string(template_list, new_context,
context_instance = context)
paginator = context['paginator']
page_obj = context['page_obj']
page_suffix = context.get('page_suffix', '')
- page_range = paginator.page_range
+ page_range = list(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
window_end = window_end - window_start
window_start = 0
if window_end > paginator.num_pages:
- window_start = window_start - (window_end - paginator.num_pages)
+ window_start = max(0, window_start - (window_end - paginator.num_pages))
window_end = paginator.num_pages
pages = page_range[window_start:window_end]
new_context = {
'MEDIA_URL': settings.MEDIA_URL,
'STATIC_URL': getattr(settings, "STATIC_URL", None),
+ 'disable_link_for_first_page': DISABLE_LINK_FOR_FIRST_PAGE,
'display_disabled_next_link': DISPLAY_DISABLED_NEXT_LINK,
'display_disabled_previous_link': DISPLAY_DISABLED_PREVIOUS_LINK,
'display_page_links': DISPLAY_PAGE_LINKS,
new_context['getvars'] = ''
return new_context
except (KeyError, AttributeError):
- new_context = {}
-
- context.update(new_context)
-
- return context
+ return {}
register = Library()