Found it. Seems like Jinja generates Python code from the Jinja AST (?) and that conversion failed, therefore caused the SyntaxError. jinja2.nodes.ContextReference()
can be used to get the render Context.
class CsrfExtension(jinja2.ext.Extension):
r""" Adds a {% csrf %} tag to Jinja. """
tags = set(['csrf', 'csrf_token'])
template = u'<input type="hidden" name="csrfmiddlewaretoken" value="%s">'
def parse(self, parser):
lineno = next(parser.stream).lineno
ctx_ref = jinja2.nodes.ContextReference()
node = self.call_method('_render_csrf', [ctx_ref], lineno=lineno)
return jinja2.nodes.CallBlock(node, [], [], [], lineno=lineno)
def _render_csrf(self, context, caller):
csrf_token = context['csrf_token']
return jinja2.Markup(self.template % unicode(csrf_token))
csrf = CsrfExtension