A GET string modifier templatetag

 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
"""
requires
TEMPLATE_CONTEXT_PROCESSORS += ('django.core.context_processors.request',)
"""


from django import template
from django.utils.http import urlquote

register = template.Library()

def do_get_string(parser, token):
   try:
       tag_name, key, value = token.split_contents()
#         key = urlquote(key)
#         value = urlquote(value)
   except ValueError:
       return GetStringNode()

   if not (key[0] == key[-1] and key[0] in ('"', "'")):
       raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name

   return GetStringNode(key[1:-1], value)

class GetStringNode(template.Node):
   def __init__(self, key=None, value=None):
       self.key = key
       if value:
           self.value = template.Variable(value)

   def render(self, context):
       get = context.get('request').GET.copy()

       if self.key:
           actual_value = self.value.resolve(context)
           get.__setitem__(self.key, actual_value)

       return get.urlencode()

register.tag('get_string', do_get_string)

More like this

  1. Add GET parameters from current request by naktinis 2 years, 11 months ago
  2. Absolute URL Templatetag by johnboxall 4 years, 11 months ago
  3. Querystring Builder - create urls with GET params by jibberia 3 years, 3 months ago
  4. Modify query string on a url by dnordberg 5 years, 10 months ago
  5. Template tag which gets specific GET variables from the current request by aruseni.magiku 4 years, 1 month ago

Comments

(Forgotten your password?)