请教各位,想引发一个这样的ValidationError错误要怎么做

请问各位,想引发一个这样的ValidationError异常要如何做?
我用django做了个留言板应用

修改时在forms.py文件中多加了一个条件,在发信息时,如果指定收信息的人不是已注册的用户(该用户不存在),则引发一个ValidationError异常.



因为基础不深,只是参考了一下别的教程, 想出了两个方法,不过似乎都不太对,希望高手给指点一下,看看是哪里的问题.

第一种:
Python code
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.')




第二种:

Python code
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.')


谢谢各位帮忙了!



------解决方案--------------------
将form验证方法clean_receiver里面的"username"换成"receiver"
你压根都没username传过来,判断"username"在不在cleaned_data里面有什么用?
我知道你为什么返回None了,因为if "username"条件不满足,又没写else,所以函数默认返回None

最后,你要再敢“无满意答案结贴”我就跟你拼了。。。

另外,你另一个“无满意答案结贴”的帖子我给你讲的东西貌似你还是没懂。

因为这个里面,同样的问题还存在。。到时候你发现这个问题解决了,错误信息居然还是显示。。。

解决这个问题了去看下您那个“无满意答案结贴”的帖子吧

Python code



    #该函数用于验证用户输入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.')

------解决方案--------------------
Python code

...
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