为了支持autoescape功能,我决定安装jinja2来与我的webapp应用程序一起使用。因此,我将jinja2安装到python2.5中,并在我的项目中创建了一个符号链接来指向该目录。它基本上运转良好。在
但是,当我实际尝试使用{%autoescape true%}标记时,我得到的消息是:
File "/Users/me/project/templates/_base.html", line 1, in template
{% autoescape true %}
TemplateSyntaxError: Encountered unknown tag 'autoescape'.
我在使用文档中的标签:
^{pr2}$在我的处理程序文件中,我导入了相关的内容:
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from jinja2.ext import autoescape
导入操作正常,因为它没有抛出错误。是我做错了什么,还是jinja2本身有问题,比如外景?在
更新: 我尝试了下面sharth的建议,得到了同样的结果。这是我的更新处理程序使用他的建议。在
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标记,它就可以正常工作。在
{% autoescape %}
标记需要jinja2.4或更高版本,并加载jinja2.ext.autoescape
扩展。在相关问题 更多 >
编程相关推荐