Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python google app engine webapp中的jinja2 autoescape出现问题_Python_Google App Engine_Jinja2 - Fatal编程技术网

Python google app engine webapp中的jinja2 autoescape出现问题

Python google app engine webapp中的jinja2 autoescape出现问题,python,google-app-engine,jinja2,Python,Google App Engine,Jinja2,我决定安装jinja2与我的webapp应用程序一起使用,以支持自动转义功能。因此,我将jinja2安装到Python2.5中,并在项目中创建了一个指向该目录的符号链接。它基本上运作良好 除此之外,当我实际尝试使用{%autoescape-true%}标记时,我得到以下消息: File "/Users/me/project/templates/_base.html", line 1, in template {% autoescape true %} TemplateSyntaxErro

我决定安装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'.
我正在使用文档中的标签:

{% autoescape true %} stuff {{var1}} stuff {{var2}}{% endautoescape %}
在我的处理程序文件中,我正在导入相关内容:

from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from jinja2.ext import autoescape
导入工作正常,因为它没有抛出错误。我是做错了什么,还是jinja2本身有问题,比如ext.py


更新: 我尝试了下面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)
class MainHandler(BaseHandler):
def get(自我):
self.context['testEscape']='alert(“hi”);'
env=Environment(loader=FileSystemLoader([os.path.join(os.path.dirname(_文件,'templates')])),autoescape=False)
template=env.get\u模板('index.html'))
content=template.render(self.context)
self.response.out.write(内容)

同样,只要我不使用autoescape标记,它就可以正常工作。

标记需要Jinja 2.4或更高版本,并且加载了扩展名
jinja2.ext.autoescape

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

我刚刚注意到jinja2自动完成标记也不像tipfy框架中所描述的那样工作。这让我觉得这是jinja2中的一个bug,而不是我如何使用它的问题。谢谢。我原以为可以在脚本顶部使用import语句导入扩展,但没有意识到在实例化环境时需要将扩展设置为参数。我想,扩展文档()顶部标题为“添加扩展”的段落应该是一个彻底的赠品。:-)实际上,autoescape的含义是什么?为什么要使用?