Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有带有 ' 和 " 字符的 CharField。当我在带有 的模板中传递此字段时{{ charfield }},它会呈现为"而不是 " 和'而不是 '。
{{ charfield }}
"
'
这种行为的根源是什么以及如何避免它?
默认情况下,进入 HTML 模板的所有变量都会自动转义,以避免通过恶意制作的用户输入产生XSS漏洞。
如果您完全确定变量的内容是安全的,您可以使用标记在块级别关闭自动转义:
{% autoescape off %} {{ charfield }} {% endautoescape %}
或带有过滤器的可变级别:
{{ charfield|safe }}