register simple_tag def alter_qs qs name value name2 None value2 None

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
@register.simple_tag
def alter_qs(qs, name, value, name2=None, value2=None):
qs = qs.lstrip('?')
args = dict((x[0], smart_str(x[1])) for x in parse_qsl(qs))
if value:
args[name] = smart_str(value)
else:
if name in args:
del args[name]
if args:
result = '?' + urlencode(args)
if name2 and value2:
return alter_qs(result, name2, value2)
else:
return result
else:
return ''