django templates模板过滤器过滤掉字符串含有的html标签
在template用法:{{ myvar|striptags }}
如果myvar的字符串是“吾爱孟夫子,风流天下闻。
红颜弃轩冕,白首卧松云。
醉月频中圣,迷花不事君。
高山安可仰,徒此揖清芬。
”,那么输出结果是:吾爱孟夫子,风流天下闻。红颜弃轩冕,白首卧松云。醉月频中圣,迷花不事君。高山安可仰,徒此揖清芬。
如果想要在python django的视图函数中使用,
在python用法:class AddressForm(forms.ModelForm):
class Meta:
model = Address
def clean(self):
from django.utils.html import strip_tags
cleaned_data = super(AddressForm, self).clean()
cleaned_data['first_name'] = strip_tags(cleaned_data.get('first_name'))
return cleaned_data