首页 > 解决方案 > Django 表单已清理

问题描述

当我点击保存按钮时,我的表单也会保存 html 元素。我的表格如下所示:

class ProductPropertyForm(forms.ModelForm):

    class Meta:
        model = ProductProperty
        fields = ('name', )

例如,如果我的文本是“我的输入文本”。表单将其保存在数据库中:从保存的数据<p>My input text</p> 中排除 html 元素的解决方案是什么?p

标签: djangodjango-modelsdjango-forms

解决方案


您可以使用这样的clean方法返回清理后的数据:

class ProductPropertyForm(forms.ModelForm):

    class Meta:
        model = ProductProperty
        fields = ('name', )


    def clean_name(self):
            cleaned_name = self.cleaned_data['name']
              
            return cleaned_name

文档


推荐阅读