For example, if I have a function called add
like
def add(x,y):
return x+y
and I want the ability to convert a string or a
I've had many situation where I've needed to compare a string to an int and vice versa within a Django template.
I created a filter that allowed me to pass in the function name and using eval() convert it.
Example:
Template:
{% ifequal string int|convert:'str' %} do something {% endifequal %}
Template Filter (where i use a string to call the function name):
@register.filter
def convert(value, funcname):
try:
converted = eval(funcname)(value)
return converted
except:
return value