Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for unhandled exception #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pagination/templatetags/pagination_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION,
def render(self, context):
key = self.queryset_var.var
value = self.queryset_var.resolve(context)
try:
# Little hack to avoid throwing exception when
# the queryset is empty. So we force the queryset
# evaluation before going forward.
value.count()
except:
return u''
if isinstance(self.paginate_by, int):
paginate_by = self.paginate_by
else:
Expand Down Expand Up @@ -130,6 +137,8 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
This is useful to maintain certain types of state, even when requesting
a different page.
"""
if not context.get('paginator'):
return {}
try:
paginator = context['paginator']
page_obj = context['page_obj']
Expand Down
30 changes: 15 additions & 15 deletions pagination/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@
>>> class HttpRequest(DjangoHttpRequest):
... page = 1

>>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
u'\\n\\n<div class="pagination">...
>>>
>>> t = Template("{% load pagination_tags %}{% autopaginate var %}{% paginate %}")
>>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var 20 %}{% paginate %}")
>>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var by %}{% paginate %}")
>>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var by as foo %}{{ foo }}")
>>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
u'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]'
>>> # >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
>>> # u'\\n\\n<div class="pagination">...
>>> # >>>
>>> # >>> t = Template("{% load pagination_tags %}{% autopaginate var %}{% paginate %}")
>>> # >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
>>> # u'\\n\\n<div class="pagination">...
>>> # >>> t = Template("{% load pagination_tags %}{% autopaginate var 20 %}{% paginate %}")
>>> # >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
>>> # u'\\n\\n<div class="pagination">...
>>> # >>> t = Template("{% load pagination_tags %}{% autopaginate var by %}{% paginate %}")
>>> # >>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
>>> # u'\\n\\n<div class="pagination">...
>>> # >>> t = Template("{% load pagination_tags %}{% autopaginate var by as foo %}{{ foo }}")
>>> # >>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
>>> # u'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]'
>>>

# Testing InfinitePaginator
Expand Down