Can't disable the autoescape in jinja2

孤者浪人 提交于 2019-12-21 07:09:12

问题


In GAE I use jinja2 with the autoescape, and everything works well.

import jinja2
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)

In one template I don't want the autoescape, so I tried to disable it like this:

{% autoescape false %}
{{content}}
{% endautoescape %}

When it's time to render this template I get the message Encountered unknown tag 'autoescape'.


回答1:


Try this:

{{ content | safe}}

docs:

  • Flask — Controlling Autoescaping
  • Jinja2 — Filters — safe



回答2:


In order for the autoescape tag to be recognized, you need to enable the autoescape extension when setting up jinja2, like this:

jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
                               autoescape = True,
                               extensions = ['jinja2.ext.autoescape'])

Also, make sure you're using jinja2 version 2.4 or higher in your app.yaml (the current version is GAE is 2.6):

libraries:
- name: jinja2
  version: "2.6"

For more information, see the documentation for the autoescape extension.



来源:https://stackoverflow.com/questions/17257138/cant-disable-the-autoescape-in-jinja2

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