Parse TemplateTag Variables Safely

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django.template import Variable, VariableDoesNotExist

QUOTED_STRING = re.compile(r'^["\'](?P<noquotes>.+)["\']$')

def handle_var(self, value, context):
  stringval = QUOTED_STRING.search(value)
  if stringval:
    return stringval.group('noquotes')
  else:
    try:    
      return Variable(value).resolve(context)
    except VariableDoesNotExist:
      return value

More like this

  1. Dynamic Regroup Template Tag by btaylordesign 1 year, 9 months ago
  2. a simple tag with context by dsblank 3 years, 4 months ago
  3. Manipulate URL query strings using context variables using a template tag by JHsaunders 2 years, 7 months ago
  4. A GET string modifier templatetag by cogat 4 years, 5 months ago
  5. Call a manager method on any model with a filter by coleifer 3 years ago

Comments

(Forgotten your password?)