Tuesday, February 28, 2017

Re: What's the best way to create a reusable pagination template?

Yep. My solution is a custom template tag

from django.template.defaulttags import register
from urllib.parse import urlencode

@register.simple_tag(takes_context=True)
def querystringmod(context, *args):
   
"""Modify current querystring:
     {% querystringmod name value [name value [...]] %}
    """

   
if (len(args) % 2) !=0:
        args
.append("")
    request
= context['request']
    current_qs
= request.GET.dict()
    current_qs
.update(dict([ (args[n], args[n+1]) for n in range(0, len(args), 2) ]))
   
return urlencode(current_qs)

in template
<a href="?{% querystringmod "page" page_obj.previous_page_number %}">previous</a>



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/31ec3240-cee2-41b5-acd0-f4e05d41fb61%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment