请问各位,想引发一个这样的ValidationError异常要如何做?
我用django做了个留言板应用
修改时在forms.py文件中多加了一个条件,在发信息时,如果指定收信息的人不是已注册的用户(该用户不存在),则引发一个ValidationError异常.
因为基础不深,只是参考了一下别的教程, 想出了两个方法,不过似乎都不太对,希望高手给指点一下,看看是哪里的问题.
第一种:
class MsgPostForm(forms.Form): receiver=forms.CharField(label='Send to:', widget=forms.TextInput (attrs={'size':30, 'max_length':30})) #实例化一个用于输入留言标题的文本框对象, #并将该对象保存到title属性中。 title=forms.CharField(label='Title', widget=forms.TextInput (attrs={'size':30, 'max_length':30})) #实例化一个用于输入留言内容的文本框对象, #并将该对象保存到content属性中。 #该文本框会转换为tinymce富文本编辑器的外观。 content=forms.CharField(label='Content', widget=forms.Textarea(attrs={'size':10000})) #该函数用于验证用户输入receiver username的合法性 def clean_receiver(self): if 'username' in self.cleaned_data: #从通过一般性验证的用户名输入到指定的receiver文本框中 #并保存到变量receiver中 receiver=self.cleaned_data['receiver'] if receiver==username: return receiver #如果该用户不存在,则引发一个ValidationError异常 raise forms.ValidationError('This user does not exist.')class MsgPostForm(forms.Form): receiver=forms.CharField(label='Send to:',max_length=30)# receiver=forms.CharField(label='Send to:',# widget=forms.TextInput (attrs={'size':30, 'max_length':30})) #实例化一个用于输入留言标题的文本框对象, #并将该对象保存到title属性中。 title=forms.CharField(label='Title', widget=forms.TextInput (attrs={'size':30, 'max_length':30})) #实例化一个用于输入留言内容的文本框对象, #并将该对象保存到content属性中。 #该文本框会转换为tinymce富文本编辑器的外观。 content=forms.CharField(label='Content', widget=forms.Textarea(attrs={'size':10000})) #该函数用于验证用户输入receiver username的合法性 def clean_receiver(self): if 'username' in self.cleaned_data: receiver=self.cleaned_data['receiver'] try: #从用户数据模型中取出username属性 #等于username变量值的用户对象 #如果在用户数据模型中不存在该用户对象 #则引发一个异常 User.objects.get(receiver!=username) #异常处理 except ObjectDoesNotExist: #返回username变量的值 return receiver #如果用户两次输入的新密码不一致,则引发一个ValidationError异常 raise forms.ValidationError('This user does not exist.') #该函数用于验证用户输入receiver username的合法性 def clean_receiver(self): #if 'username' in self.cleaned_data: #------这里错了--------- if 'receiver' in self.cleaned_data: receiver=self.cleaned_data['receiver'] try: #从用户数据模型中取出username属性 #等于username变量值的用户对象 #如果在用户数据模型中不存在该用户对象 #则引发一个异常 User.objects.get(receiver!=username) #异常处理 except ObjectDoesNotExist: #返回username变量的值 return receiver #如果用户两次输入的新密码不一致,则引发一个ValidationError异常 raise forms.ValidationError('This user does not exist.')
[解决办法]
...import pprint class MsgPostForm(forms.Form): receiver=forms.CharField(label='Send to:',max_length=30) title=forms.CharField(label='Title', widget=forms.TextInput (attrs={'size':30, 'max_length':30})) content=forms.CharField(label='Content', widget=forms.Textarea(attrs={'size':10000})) def clean_receiver(self): pprint.pprint(self.cleaned_data) # print self.cleaned_data try: User.objects.get(username = self.cleaned_data['receiver']) return self.cleaned_data['receiver'] except User.DoesNotExist: return "UNKNOWN USER" # if there is no such user, set self.cleaned_data['receiver'] to # "UNKNOWN USER" # or you can raised an exception error without returning "UNKNOWN USER" # so that it wont be saved #raise forms.ValidationError('This user does not exist.') raise forms.ValidationError('This user does not exist.') def clean(self): pprint.pprint(self.cleaned_data) if 'username' in self.cleaned_data and 'receiver' in self.cleaned_data: if self.cleaned_data['username'] == self.cleaned_data['receiver']: raise forms.ValidationError('user = receiver ') return self.cleaned_data