problem with jinja2 autoescape in google app engine webapp

狂风中的少年 提交于 2019-12-22 06:55:44

问题


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.


回答1:


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=...)


来源:https://stackoverflow.com/questions/4674366/problem-with-jinja2-autoescape-in-google-app-engine-webapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!