1
jokaye 2013-10-16 14:32:27 +08:00
class YourForm(ModelForm):
def __init__(self, *args, **kwargs): super(YourForm, self).__init__(*args, **kwargs) self.error_message = 'whatever you want' def clean_somefield(slef): if not check_somefield_valid(): //do your check in here raise formValidateError(self.error_message) return self |
2
halicando OP @jokaye
好像还是不行 版本信息:django1.3.1 ,python2.7.1 以下是代码: class TopicForm(forms.Form): title=forms.CharField(max_length=50,label='标题') content=forms.CharField(max_length=300,widget=forms.Textarea(),label='内容') def __init__(self, *args, **kwargs): super(TopicForm, self).__init__(*args, **kwargs) self.error_message = u'不超过300字' def clean_title(self): title = self.cleaned_data['title'] if len(title) > 50: raise forms.ValidationError(u'不超过50字') return title def clean_content(self): content = self.cleaned_data['content'] if len(content) > 300: raise forms.ValidationError(self.error_message) return content |