Django密码和密码确认验证(Django password and password confirmation validation)

编程入门 行业动态 更新时间:2024-10-26 23:25:53
Django密码和密码确认验证(Django password and password confirmation validation)

我在同一表格中验证两个字段(密码和密码确认)时遇到了一些麻烦。

问题是,在使用我创建的方法验证密码后,当我尝试验证密码确认时,我无法再访问此变量,并且password = self.cleaned_data['password']为'None' 。

class NewAccountForm(forms.Form): password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'narrow-input', 'required': 'true' }), required=True, help_text='Password must be 8 characters minimum length (with at least 1 lower case, 1 upper case and 1 number).') password_confirm = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'narrow-input', 'required': 'true' }), required=True, ) def __init__(self, *args, **kwargs): super(NewAccountForm, self).__init__(*args, **kwargs) self.fields['password'].label = "Password" self.fields['password_confirm'].label = "Password Confirmation"

“密码验证”< - 此验证正常。

def clean_password(self): validate_password_strength(self.cleaned_data['password'])

第二次验证未正确执行,因为password ='None':

def clean_password_confirm(self): password = self.cleaned_data['password'] password_confirm = self.cleaned_data.get('password_confirm') print(password) print(password_confirm) if password and password_confirm: if password != password_confirm: raise forms.ValidationError("The two password fields must match.") return password_confirm

如果已经通过第一种方法(clean_password)验证了,那么有没有办法将字段密码输入用作第二次验证(clean_password_confirm)的变量?

谢谢。

编辑:更新版本:

def clean(self): cleaned_data = super(NewAccountForm, self).clean() password = cleaned_data.get('password') # check for min length min_length = 8 if len(password) < min_length: msg = 'Password must be at least %s characters long.' %(str(min_length)) self.add_error('password', msg) # check for digit if sum(c.isdigit() for c in password) < 1: msg = 'Password must contain at least 1 number.' self.add_error('password', msg) # check for uppercase letter if not any(c.isupper() for c in password): msg = 'Password must contain at least 1 uppercase letter.' self.add_error('password', msg) # check for lowercase letter if not any(c.islower() for c in password): msg = 'Password must contain at least 1 lowercase letter.' self.add_error('password', msg) password_confirm = cleaned_data.get('password_confirm') if password and password_confirm: if password != password_confirm: msg = "The two password fields must match." self.add_error('password_confirm', msg) return cleaned_data

I'm having a little trouble validating two fields (password and password confirmation) in the same form.

The thing is that after validating password with a method I've created, when I try to validate password confirmation, I no longer have access to this variable, and password = self.cleaned_data['password'] is 'None'.

class NewAccountForm(forms.Form): password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'narrow-input', 'required': 'true' }), required=True, help_text='Password must be 8 characters minimum length (with at least 1 lower case, 1 upper case and 1 number).') password_confirm = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'narrow-input', 'required': 'true' }), required=True, ) def __init__(self, *args, **kwargs): super(NewAccountForm, self).__init__(*args, **kwargs) self.fields['password'].label = "Password" self.fields['password_confirm'].label = "Password Confirmation"

"Password validation" <- this validation is working.

def clean_password(self): validate_password_strength(self.cleaned_data['password'])

This second validation isn't correctly performed because password = 'None':

def clean_password_confirm(self): password = self.cleaned_data['password'] password_confirm = self.cleaned_data.get('password_confirm') print(password) print(password_confirm) if password and password_confirm: if password != password_confirm: raise forms.ValidationError("The two password fields must match.") return password_confirm

Is there a way to use input for field password as a variable to the second validation (clean_password_confirm) if it is already validated by the first method (clean_password)?

Thanks.

EDIT: Updated version:

def clean(self): cleaned_data = super(NewAccountForm, self).clean() password = cleaned_data.get('password') # check for min length min_length = 8 if len(password) < min_length: msg = 'Password must be at least %s characters long.' %(str(min_length)) self.add_error('password', msg) # check for digit if sum(c.isdigit() for c in password) < 1: msg = 'Password must contain at least 1 number.' self.add_error('password', msg) # check for uppercase letter if not any(c.isupper() for c in password): msg = 'Password must contain at least 1 uppercase letter.' self.add_error('password', msg) # check for lowercase letter if not any(c.islower() for c in password): msg = 'Password must contain at least 1 lowercase letter.' self.add_error('password', msg) password_confirm = cleaned_data.get('password_confirm') if password and password_confirm: if password != password_confirm: msg = "The two password fields must match." self.add_error('password_confirm', msg) return cleaned_data

最满意答案

您可以在clean()方法中测试多个字段。

例:

def clean(self): cleaned_data = super(NewAccountForm, self).clean() password = cleaned_data.get('password') password_confirm = cleaned_data.get('password_confirm ') if password and password_confirm: if password != password_confirm: raise forms.ValidationError("The two password fields must match.") return cleaned_data

请参阅有关清洁和验证彼此依赖的字段的文档。

You can test for multiple fields in the clean() method.

Example:

def clean(self): cleaned_data = super(NewAccountForm, self).clean() password = cleaned_data.get('password') password_confirm = cleaned_data.get('password_confirm ') if password and password_confirm: if password != password_confirm: raise forms.ValidationError("The two password fields must match.") return cleaned_data

See the documentation on Cleaning and validating fields that depend on each other.

更多推荐

本文发布于:2023-07-31 00:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340431.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密码   Django   password   validation   confirmation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!