Python flask应用程序未正确呈现html标记

Python flask应用程序未正确呈现html标记,python,flask,markup,Python,Flask,Markup,我有一个句子 sentence = <p> Reading, watching or <span class="matching">listening</span> to the media isn’t <span class="matching">matching</span><span class="matching">much</span> help either. </p> 但是,只为一个

我有一个句子

sentence =  <p> Reading, watching or <span class="matching">listening</span> to the media isn’t <span class="matching">matching</span><span class="matching">much</span> help either. </p>
但是,只为一个标记(不一定是第一个标记)正确地呈现输出,而其他标记则不被呈现

            <p> Reading, watching or <span class="matching">listening</span> to the media isn’t &lt;span class=&#34;matching&#34;&gt;much&lt;/span&gt; help either. </p>
阅读、观看或收听媒体不是span class=";匹配";也有很多帮助

我在这里做错了什么?

罪魁祸首是

不是吗

“'”不是有效的ASCII,因为它不在HTML标记的有效字符范围内,因此它会转义它(尽管它应该抛出一个错误)

希望这能解决问题

这对我有用

from flask import Markup
sentence =  '<p> Reading, watching or <span class="matching">listening</span> to the media isn\'t <span class="matching">matching</span><span class="matching">much</span> help either. </p>'
Markup(sentence)
从flask导入标记
阅读、观看或收听媒体也没有多大帮助

" 标记(句子)
返回

Markup(u'<p> Reading, watching or <span class="matching">listening</span> to the media isn\'t <span class="matching">matching</span><span class="matching">much</span> help either. </p>')
标记(阅读、观看或收听媒体也没有太大帮助。


希望这就是django中所需的输出

我们会这样做:{{variable_name | safe}}我在终端中尝试您的示例时遇到以下错误:“UnicodeDecodeError:'ascii'编解码器无法解码位置81处的字节0xe2:序号不在范围(128)”您没有收到这样的错误吗?下面是解决这个问题的SO链接:谢谢@rajpy。你发布的链接有一个函数,可以删除所有非ascii字符,这使它变得更容易。谢谢:)好的。您看到了罪魁祸首,并且手动编辑并添加了escape,使其成为有效的HTML标记。但我有无数的句子。如何首先将这些句子更改为有效的HTML标记?好吧,您可以编写一个脚本来删除超出范围的所有字符。。或将其替换为有效的..:D
Markup(u'<p> Reading, watching or <span class="matching">listening</span> to the media isn\'t <span class="matching">matching</span><span class="matching">much</span> help either. </p>')