擅长:python、mysql、java
<p>找到了。似乎Jinja从Jinja AST(?)生成Python代码而这种转换失败了,因此导致了语法错误。<code>jinja2.nodes.ContextReference()</code>可用于获取呈现上下文。在</p>
<pre><code>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
</code></pre>