Skip to content

Commit e2a7174

Browse files
committed
Chants: pagination: preserve filter parameters
1 parent 4db4245 commit e2a7174

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

scores/templates/scores/_pagination.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
{% load params %}
2+
13
{% if page_obj %}
24
<nav aria-label="Page navigation" class="mt-4">
35
<ul class="pagination justify-content-center">
46
{% if page_obj.has_previous %}
57
<li class="page-item">
6-
<a class="page-link" href="?page=1" aria-label="First">
8+
<a class="page-link" href="?{% query_transform page=1 %}" aria-label="First">
79
<span aria-hidden="true">&laquo;&laquo;</span>
810
</a>
911
</li>
1012
<li class="page-item">
11-
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
13+
<a class="page-link" href="?{% query_transform page=page_obj.previous_page_number %}" aria-label="Previous">
1214
<span aria-hidden="true">&laquo;</span>
1315
</a>
1416
</li>
@@ -21,19 +23,19 @@
2123
</li>
2224
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
2325
<li class="page-item">
24-
<a class="page-link" href="?page={{ num }}">{{ num }}</a>
26+
<a class="page-link" href="?{% query_transform page=num %}">{{ num }}</a>
2527
</li>
2628
{% endif %}
2729
{% endfor %}
2830

2931
{% if page_obj.has_next %}
3032
<li class="page-item">
31-
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
33+
<a class="page-link" href="?{% query_transform page=page_obj.next_page_number %}" aria-label="Next">
3234
<span aria-hidden="true">&raquo;</span>
3335
</a>
3436
</li>
3537
<li class="page-item">
36-
<a class="page-link" href="?page={{ page_obj.paginator.num_pages }}" aria-label="Last">
38+
<a class="page-link" href="?{% query_transform page=page_obj.paginator.num_pages %}" aria-label="Last">
3739
<span aria-hidden="true">&raquo;&raquo;</span>
3840
</a>
3941
</li>

scores/templatetags/__init__.py

Whitespace-only changes.

scores/templatetags/params.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# credits: https://gist.github.com/benbacardi/d6cd0fb8c85e1547c3c60f95f5b2d5e1
2+
from django import template
3+
4+
register = template.Library()
5+
6+
7+
@register.simple_tag(takes_context=True)
8+
def query_transform(context, **kwargs):
9+
'''
10+
Returns the URL-encoded querystring for the current page,
11+
updating the params with the key/value pairs passed to the tag.
12+
13+
E.g: given the querystring ?foo=1&bar=2
14+
{% query_transform bar=3 %} outputs ?foo=1&bar=3
15+
{% query_transform foo='baz' %} outputs ?foo=baz&bar=2
16+
{% query_transform foo='one' bar='two' baz=99 %} outputs ?foo=one&bar=two&baz=99
17+
18+
A RequestContext is required for access to the current querystring.
19+
'''
20+
query = context['request'].GET.copy()
21+
for k, v in kwargs.items():
22+
query[k] = v
23+
return query.urlencode()

0 commit comments

Comments
 (0)