[英]Django template question: how to output just the text if the variable has html in it?
我有很多變量,其中包含html。 例如,名為{{object.name}}的變量的值如下:
Play this <a href="#">hot</a> game and see how <b>fun</b> it is!
是否有一個可以應用於變量的過濾器,它只給出了文本:
Play this hot game and see how fun it is!
沒有文本鏈接或html被htmlentities取代。 只是文字?
striptags過濾器刪除所有html
{{object.name | striptags}}
您有3個選項來刪除HTML代碼:
在模板中使用“ 安全 ”過濾器:
{{ object.name|safe }}
在模板中使用“ autoescape ”標記:
{% autoescape off %}
{{ object.name }}
{% endautoescape %}
或者在python代碼中將其聲明為“ 安全 ”:
from django.utils.safestring import mark_safe
name = mark_safe(name)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.