This tag is designed to facilitate pagination in the case where both the page number and other parameters (eg. search criteria) are passed via GET.
It takes one argument - a dictionary of GET variables to be added to the current url
Example usage:
{% for page_num in results.paginator.page_range %}
<a href="{% append_to_get p=page_num %}">{{ page_num }}</a>
{% endfor %}
Note that the passed arguments are evaluated within the template context.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | from django import template
register = template.Library()
"""
Decorator to facilitate template tag creation
"""
def easy_tag(func):
"""deal with the repetitive parts of parsing template tags"""
def inner(parser, token):
#print token
try:
return func(*token.split_contents())
except TypeError:
raise template.TemplateSyntaxError('Bad arguments for tag "%s"' % token.split_contents()[0])
inner.__name__ = func.__name__
inner.__doc__ = inner.__doc__
return inner
class AppendGetNode(template.Node):
def __init__(self, dict):
self.dict_pairs = {}
for pair in dict.split(','):
pair = pair.split('=')
self.dict_pairs[pair[0]] = template.Variable(pair[1])
def render(self, context):
get = context['request'].GET.copy()
for key in self.dict_pairs:
get[key] = self.dict_pairs[key].resolve(context)
path = context['request'].META['PATH_INFO']
#print "&".join(["%s=%s" % (key, value) for (key, value) in get.items() if value])
if len(get):
path += "?%s" % "&".join(["%s=%s" % (key, value) for (key, value) in get.items() if value])
return path
@register.tag()
@easy_tag
def append_to_get(_tag_name, dict):
return AppendGetNode(dict)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 4 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Oops, thanks for pointing that out.
#
Thanks! I was just about to do something similar.
#
Thanks, very handy. Just a note, I had to change line 40 to:
to get things to work correctly in the presence of QueryDicts with multiple values for the same key.
#
Thanks, very handy. Just a note, I had to change line 40 to:
to get things to work correctly in the presence of QueryDicts with multiple values for the same key.
#
You need to have the request context processor.
#
class AppendGetNode(template.Node): def init(self, d): self.dict_pairs = [] for pair in d.split(','): k, v = pair.split('=') self.dict_pairs.append((k, template.Variable(v)))
#
Please login first before commenting.