我有一个这样的模板:
{% extends "index.html" %}
{% block content %}
{% if post %}
<p> Post id: {{post.id}} </p>
Post title: {{post.post_title}}
Post: {{post.post_body | safe}}
{% endif %}
{% endblock content %}
我的 post.post_body
是
<code> Hello World! </code>
问题是,当页面被呈现时,代码标签也会出现,但它们并没有呈现为 HTML 标签。如果我检查呈现的 html,我会看到:
<p><code> Hello World! </code></p>
如何将这些标签呈现为 HTML 标签?
最佳答案
除了使用 safe
过滤器,您应该使用标签 {% autoescape off %}
和 {% endautoescape %}
包装变量。
关于python - HTML 代码在模板中显示为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28250211/