google app engine webapp中jinja2 autoescape出现问题

2024-05-29 00:21:59 发布

您现在位置:Python中文网/ 问答频道 /正文

为了支持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标记,它就可以正常工作。在


Tags: from标记importself处理程序jinja2getenvironment
1条回答
网友
1楼 · 发布于 2024-05-29 00:21:59

{% autoescape %}标记需要jinja2.4或更高版本,并加载jinja2.ext.autoescape扩展。在

env = Environment(autoescape=True, extensions=['jinja2.ext.autoescape'],
                  loader=...)

相关问题 更多 >

    热门问题