繁体   English   中英

Django 奇怪地解释安全过滤器

[英]Django interprets safe filter weirdly

我们有一个 django 模板,但是我对富文本数据有一个奇怪的问题。

例子:

模板: <p class="correctClass">{{team.description|safe}}</p>

我们 CMS 中team.description的源代码: <p>Correct text</p>

结果:

<p class="correctClass"></p>
<p>Correct text</p>
<p></p>

没有安全过滤器是这样的:

<p class="correctClass">
    <p>Correct text</p> (this line is text, not parsed as html)
</p>

当然想要 output 是:

<p class="correctClass">Correct text</p>

如果你想实现这一点,你可以使用striptags

<p class="correctClass">{{team.description|striptags}}</p>

" Note that striptags doesn't give any guarantee about its output being HTML safe, particularly with non valid HTML input. So NEVER apply the safe filter to a striptags output. If you are looking for something more robust, you can use the bleach Python库,尤其是它的干净方法。 ”来自官方文档
有关更多信息,请参阅https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#striptags

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM