2011-06-15 20 views
4

我有很多變量,其中有HTML。例如,一個名爲變量的值{{object.name}}如下:Django模板問題:如果變量中包含html,如何輸出文本?

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! 

沒有被鏈接的文本或者被htmlentities替換的html。只是文字?

回答

1

你有3個選擇脫光html代碼:

使用模板中的 「safe」 過濾器:

{{ object.name|safe }} 

使用模板中的 「autoescape」 標籤:

{% autoescape off %} 
{{ object.name }} 
{% endautoescape %} 

或Python代碼,宣佈其爲 「safe」:

from django.utils.safestring import mark_safe 
name = mark_safe(name) 
相關問題