本文介绍了使用 Flask 将 HTML 文本打印为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个小片段,用于打印包含一些html标签以及<strong></strong>的文本块:

Following is a small snippet that prints a chunk of text that includes some html tags as well like <strong></strong>:

{% if doc.highlight %}
    {% for entry in doc.highlight.text %}
        {{entry}}<br>
    {% endfor %}
{% endif %}

{{entry}} 是具有文本块的变量,例如 找到最佳(最低速率)唯一<strong>可解码</strong>,可变长度熵.这也将打印带有 HTML 标签的文本!!

{{entry}} is the variable that has the chunk of text like finds the optimum (least rate) uniquely <strong>decodable</strong>, variable length entropy. This will print the text with the HTML tags also!!

我该如何解决这个问题?

How should I solve this problem?

推荐答案

因为 Jinja2 会自动转义 HTML 标签,要禁用它,请尝试:

{% if doc.highlight %}
    {% for entry in doc.highlight.text %}
        {{ entry|safe }}<br>
    {% endfor %}
{% endif %}

安全过滤器

[标记] 该值是安全的,这意味着在启用自动转义的环境中,该变量不会被转义.

这篇关于使用 Flask 将 HTML 文本打印为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:15
查看更多