You can do it in a custom eval-like tag:
@register.tag()
def evalpy(parser, token):
try:
tag_name, expression = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires a
single argument" % token.contents.split()[0]
if not (expression[0] == expression[-1] and expression[0] in
('"', "'")):
raise template.TemplateSyntaxError, "%r tag's argument
should be in quotes" % tag_name
return EvalNode(expression[1:-1])
class EvalNode(template.Node):
def __init__(self, expression):
self.expression = expression
def render(self, context):
return eval(self.expression, {}, context)
and then use it like below. Note you can access context/template
variables this way:
{% get_comment_count for post as comment_count %}
<h1>{% evalpy "comment_count + 3" %}</h1>
Kludgy, yes, but sometimes useful.
-Dave