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)
Comments
What about
???
#
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)))
#