在谷歌应用程序引擎webapp中jinja2 autoescape的问题 [英] problem with jinja2 autoescape in google app engine webapp
问题描述
我决定安装jinja2以与我的web应用程序一起使用,以支持autoescape功能。所以我将jinja2安装到python 2.5中,并在我的项目中创建了一个指向该目录的符号链接。它基本上工作正常。
除了当我真正尝试使用{%autoescape true%}标记时,我收到以下消息:
模板
{%autoescape true%} $ b $中的文件/Users/me/project/templates/_base.html,第1行b TemplateSyntaxError:遇到未知标签'autoescape'。
我在文档中使用标签:
{%autoescape true%} stuff {{var1}} stuff {{var2}} {%endautoescape%}
在我的处理程序文件中,我正在导入相关的内容:
from jinja2导入环境,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__), '模板')]),autoescape = FALSE)
模板= env.get_template( '的index.html')
的含量= template.render(self.context)
self.response.out.write(content)
<
{%autoescape%}
标记需要Jinja 2.4或更高版本以及 jinja2.ext.autoescape
扩展程序。
env = Environment(autoescape = True,extensions = ['jinja2.ext.autoescape'],$ b $ loader = ...)
I decided to install jinja2 to use with my webapp application in order to support the autoescape functionality. So I installed jinja2 into python 2.5 and created a symlink within my project to point to that directory. It's mostly working fine.
EXCEPT, when I actually try to use the {% autoescape true %} tag, I get the message:
File "/Users/me/project/templates/_base.html", line 1, in template
{% autoescape true %}
TemplateSyntaxError: Encountered unknown tag 'autoescape'.
I'm using the tags as they have it in the docs:
{% autoescape true %} stuff {{var1}} stuff {{var2}}{% endautoescape %}
Within my handler file I'm importing the relevant stuff:
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from jinja2.ext import autoescape
And the import is working fine because it's not throwing an error. Am I doing something wrong, or is there a problem with jinja2 itself, like maybe in ext.py?
UPDATE: I tried sharth's suggestion below and got the same result. Here is my updated handler using his suggestion.
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)
Again, it works fine as long as I don't use the autoescape tag.
The {% autoescape %}
tag needs Jinja 2.4 or higher and the jinja2.ext.autoescape
extension loaded.
env = Environment(autoescape=True, extensions=['jinja2.ext.autoescape'],
loader=...)
这篇关于在谷歌应用程序引擎webapp中jinja2 autoescape的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!