Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将2个值传递给验证器_Python_Django_Passwords_Customvalidator - Fatal编程技术网

Python 将2个值传递给验证器

Python 将2个值传递给验证器,python,django,passwords,customvalidator,Python,Django,Passwords,Customvalidator,在Django中,我尝试在表单中使用compare password和confirm_password创建验证器,但我不想使用clean方法。我想要我的do-cystom验证器,并将其放入widget-confirm\u密码字段 我不知道如何将两个值的密码和确认密码传递给我的验证器 def validate_test(): cleaned_data = super(UserSignUpForm, self).clean() password = cleaned_data.get(

在Django中,我尝试在表单中使用compare password和confirm_password创建验证器,但我不想使用clean方法。我想要我的do-cystom验证器,并将其放入widget-confirm\u密码字段

我不知道如何将两个值的密码和确认密码传递给我的验证器

def validate_test():
    cleaned_data = super(UserSignUpForm, self).clean()
    password = cleaned_data.get("password")
    confirm_password = cleaned_data.get("confirm_password")
    print(f'password:{password}\nconfirm_password: {confirm_password}')

    if password != confirm_password:
        raise ValidationError(
            _('%(password)s and %(confirm_password)s does not match - test'),
            params={'password': password, 'confirm_password': confirm_password},
        )


class UserSignUpForm(forms.ModelForm):
   
    password = forms.CharField(
        label="Password",
        validators=[MinLengthValidator(8, message="Zbyt krótkie hasło, min. 8 znaków")],
        widget=forms.PasswordInput(attrs={'style':'max-width: 20em; margin:auto', 'autocomplete':'off'}))
    confirm_password = forms.CharField(
        label="Confirm password",
        validators=[MinLengthValidator(8, message="Zbyt krótkie hasło, min. 8 znaków"), validate_test()],
        widget=forms.PasswordInput(attrs={'style':'max-width: 20em; margin:auto', 'autocomplete':'off'}))

    class Meta:
        model = User
        fields = ("username", 'first_name', 'last_name', "password")
        help_texts = {"username": None}
        widgets = {
            'username': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),
            'first_name': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),
            'last_name': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),

        }
不,我在网站上有不同的消息

您可以在表单中添加()方法:

class UserSignUpForm(forms.ModelForm):
   
    ...
    
    def clean(self):
        cleaned_data = super().clean()
        password = cleaned_data.get("password")
        confirm_password = cleaned_data.get("confirm_password")
        print(f'password:{password}\nconfirm_password: {confirm_password}')

        if password != confirm_password:
            msg = _('%(password)s and %(confirm_password)s does not match - test') % {
                'password': password, 'confirm_password': confirm_password
            })
            # raise ValidationError(msg)
            # or use add_error()
            self.add_error('password', msg)
            self.add_error('confirm_password', msg)
class UserSignUpForm(forms.ModelForm):
...
def清洁(自清洁):
cleaned_data=super().clean()
password=已清理的数据。获取(“密码”)
确认密码=已清理的数据。获取(“确认密码”)
打印(f'密码:{password}\n确认密码:{confirm\u password}')
如果输入密码!=确认密码:
msg=u('%(密码)s和%(确认密码)s不匹配-测试')%{
“密码”:密码,“确认密码”:确认密码
})
#raise ValidationError(消息)
#或者使用add_error()
self.add_错误('password',msg)
self.add\u错误('confirm\u password',msg)
Django还建议:

我们一次对多个字段执行验证,因此 表单的clean()方法是一个很好的方法。请注意,我们是 这里讨论表单上的clean()方法,而之前我们 正在对字段编写clean()方法。保持健康是很重要的 在确定验证位置时,字段和表单的差异是明确的 东西。字段是单个数据点,表单是 田地


另请参见

我不希望它以干净的方式出现在表单中,因为在前面的web站点中,我有不同的验证消息和小部件中的验证。我编辑了这篇文章。@darek_82,你可以使用
self.add_error('password',msg)
self.add_error('confirm_password',msg)
将错误添加到字段。@darek_82,我编辑了我的答案。是的,就是它!谢谢:)