dajngo 表單


django 表單驗證django

驗證順序爲 顯示form 表單定義時的驗證,在是form表單字段驗證,最後是form表單驗證
對某個字段驗證
    def clean_abstract(self):
        abstract = self.cleaned_data.get("abstract")
        if len(abstract) > 3:
            raise forms.ValidationError((u'abstract 不能>3個字段'))
        return abstractorm

對整個form驗證
    def clean(self):
        cleaned_data = super(NewPost, self).clean()
        title = cleaned_data.get("title")
        content = cleaned_data.get("content")
        if len(title)<6 and len(content)<6:
            raise forms.ValidationError((u'title and content 不能同時小於6個字段'))
        return cleaned_dataget

相關文章
相關標籤/搜索