使用 Flask/Jinja2 将 HTML 传递给模板 [英] Passing HTML to template using Flask/Jinja2
问题描述
我正在为 Flask 和 SQLAlchemy 构建管理员,我想使用 render_template
将不同输入的 HTML 传递到我的视图.模板框架似乎会自动转义 HTML,因此所有 <<<>
字符都转换为 HTML 实体.如何禁用它以便 HTML 正确呈现?
I'm building an admin for Flask and SQLAlchemy, and I want to pass the HTML for the different inputs to my view using render_template
. The templating framework seems to escape the HTML automatically, so all <"'>
characters are converted to HTML entities. How can I disable that so that the HTML renders correctly?
推荐答案
要在呈现值时关闭自动转义,请使用 |safe
过滤器.
To turn off autoescaping when rendering a value, use the |safe
filter.
{{ something|safe }}
仅对您信任的数据执行此操作,因为在不转义的情况下呈现不受信任的数据是一种跨站点脚本漏洞.
Only do this on data you trust, since rendering untrusted data without escaping is a cross-site scripting vulnerability.
这篇关于使用 Flask/Jinja2 将 HTML 传递给模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!