我决定安装jinja2以与我的web应用程序一起使用,以支持autoescape功能。所以我将jinja2安装到python 2.5中,并在我的项目中创建了一个指向该目录的符号链接。它大多工作正常。谷歌应用引擎webapp中的jinja2 autoescape问题
EXCEPT,当我真正尝试使用{%autoescape真正%}标签,我得到的消息:
File "/Users/me/project/templates/_base.html", line 1, in template
{% autoescape true %}
TemplateSyntaxError: Encountered unknown tag 'autoescape'.
我使用的标签,因为他们有它在docs:
{% autoescape true %} stuff {{var1}} stuff {{var2}}{% endautoescape %}
在我的处理程序文件,我导入相关的东西:
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from jinja2.ext import autoescape
,并在导入工作正常,因为它是不会抛出错误。我做错了什么,或者jinja2本身有问题,就像ext.py?
更新: 我试过下面沙尔斯的建议,并得到了相同的结果。这是我使用他的建议更新处理程序。
class MainHandler(BaseHandler):
def get(self):
self.context['testEscape']='<script type="javascript">alert("hi");</script>'
env = Environment(loader=FileSystemLoader([os.path.join(os.path.dirname(__file__), 'templates')]), autoescape=False)
template = env.get_template('index.html')
content = template.render(self.context)
self.response.out.write(content)
同样,只要我不使用autoescape标记,它就可以正常工作。
我刚刚注意到,jinja2自动完成标签不能像tipfy框架中记录的那样工作。这导致我认为这是jinja2中的一个bug,而不是我如何使用它的问题。 – 2011-01-12 23:10:27