简体   繁体   中英

Django interprets safe filter weirdly

We have a django template however I have a strange problem with data that is a rich-text.

Example:

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

source code for team.description in our CMS: <p>Correct text</p>

result:

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

without safe filter it's like that:

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

Of course wanted output is:

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

if you want to achieve that you can use the 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 library, notably its clean method. " From the official documentation
for more informations refer to https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#striptags

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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