My custom template tag is called change_page and should change page=x in the url without changing anything else in the url. (That means anything in the url, before and behind the ?.
That's the template code part: (it works with the standard django paginator)
{% if page_obj.has_previous %}
<a href="{{ request.path }}?{% change_page current=request.GET.urlencode page=page_obj.previous_page_number %}" class="left_sharp">Zurück</a>
{% else %}
<a tabindex="2" href="" class="noteditable">Zurück</a>
{% endif %}
<div class="screen_only">
{% for i in paginator.page_range %}
{% ifequal page_obj.number i %}
<a href="" tabindex="2" class="noteditable">{{ i }}</a>
{% else %}
<a href="{{ request.path }}?{% change_page current=request.GET.urlencode page=i %}">{{ i }}</a>
{% endifequal %}
{% endfor %}
</div>
{% if page_obj.has_next %}
<a href="{{ request.path }}?{% change_page current=request.GET.urlencode page=page_obj.next_page_number %}" class="right_sharp">Weiter</a>
{% else %}
<a href="" tabindex="2" class="noteditable">Weiter</a>
{% endif %}
</nav>
{% endif %}
The code of the template tag change_page:
@register.simple_tag
def change_page(current="", page=""):
args = current.split('&')
all=''
for arg in args:
all += re.sub(r'page=[0-9]+', 'page='+str(page), arg) + '&'
all = all[:-1]
return all
It doesn't work. It allways returns an empty string. What did I wrong?
It didn't worked because I did nothing when there was no page argument in request.GET.urlencode.
I added this code in the beginning of the method, and now it works fine unless it adds a = to arguments which are only keys, and have no values.
if current == "":
return 'page='+str(page)
if not re.compile('page=[0-9]+').match(current):
return current + '&page=' + str(page)