class RegisterForm(Form):
    # user_id=fields.IntegerField()
    username=fields.CharField(
        max_length=12,
        min_length=4,
        required=True,
        error_messages={'required':'用户名不能为空','invalid':'输入不合规'},
        # widget = widgets.TextInput(attrs={'class': 'form-control loon luser'})
        widget = widgets.TextInput(attrs={'class': 'form-control loon luser','value':'用户名'})
    )
    password = fields.CharField(
        required=True,
        max_length=32,
        min_length=6,
        error_messages={'required': '密码不能为空', 'invalid': '输入不合规'},
        # widget=widgets.TextInput(attrs={'class': 'form-control loon lpass'})
        widget=widgets.TextInput(attrs={'class': 'form-control loon lpass', 'value': '密码'})
    )
    password_confum = fields.CharField(
        required=True,
        max_length=32,
        min_length=6,
        error_messages={'required': '密码不能为空', 'invalid': '输入不合规'},
        # widget=widgets.TextInput(attrs={'class': 'form-control loon lpass'})
        widget=widgets.TextInput(attrs={'class': 'form-control loon lpass', 'value': '密码'})
    )
    def clean(self):
        print(self.cleaned_data['password'])
        print(self.cleaned_data['password_confum'])
        if self.cleaned_data['password'] == self.cleaned_data['password_confum']:
            return self.cleaned_data
        else:
            self.add_error('password_confum',ValidationError('密码不一致'))
            return self.cleaned_data
通过 clean 函数