开发者

problem with jinja2 autoescape in google app engine webapp

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, TemplateN开发者_开发问答otFound
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=...)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜