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 page URI: do not lose search query when changing page #109

Merged
merged 2 commits into from Nov 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions membership/templates/membership/paginating_snippet.html
@@ -1,21 +1,23 @@
{% load i18n %}
{% load page %}

{% if is_paginated %}
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}{% if search_query %}&query={{ search_query }}{% endif %}{% if sort %}&sort={{ sort }}{% endif %}">{% trans "Previous" %}</a>
<a href="{% page "previous" %}">{% trans "Previous" %}</a>
{% endif %}

{% for pagenum in paginator.page_range %}
{% ifnotequal pagenum page %}
<a href="?page={{ pagenum }}{% if search_query %}&query={{ search_query }}{% endif %}{% if sort %}&sort={{ sort }}{% endif %}">{{ pagenum }}</a>
<a href="{% page pagenum %}">{{ pagenum }}</a>
{% else %}
<strong>{{ pagenum }}</strong>
{% endifnotequal %}
{% endfor %}

{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}{% if search_query %}&query={{ search_query }}{% endif %}{% if sort %}&sort={{ sort }}{% endif %}">{% trans "Next" %}</a>
<a href="{% page "next" %}">{% trans "Next" %}</a>
{% endif %}
</span>
</div>
Expand Down
42 changes: 42 additions & 0 deletions membership/templatetags/page.py
@@ -0,0 +1,42 @@
from __future__ import unicode_literals

from django import template
from django.http import QueryDict

register = template.Library()


class Page(template.Node):
def __init__(self, field):
self.page = field.replace('"', '')

def render(self, context):
"""Return querystring part of URI
>>> p = Page("5")
>>> p.render({'querystring': {}})
u'?page=5'
>>> p.render({'querystring': {'sort':'id:1'}})
u'?sort=id:1&page=5'
"""
querystring = context.get('querystring', {})
if isinstance(querystring, QueryDict):
querystring = querystring.dict()
page_obj = context.get('page_obj')
if page_obj is None:
querystring['page'] = unicode(self.page)
elif self.page == 'previous':
querystring['page'] = str(page_obj.previous_page_number())
elif self.page == 'next':
querystring['page'] = str(page_obj.next_page_number())
else:
querystring['page'] = str(context.get(self.page))
return "?" + "&".join(["=".join(k) for k in querystring.items()])


def do_page(parser, token):
"""Get sorturl by sort field"""
tag_name, field = token.split_contents()
return Page(field)


register.tag('page', do_page)