问题
I'm doing a little password confirmation form in django. But Im confused as the self.cleaned_data.get('confirm_password')
always returns none and hence my passwords never match to one another.
Heres the clean
method in the forms.py
def clean_password(self):
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if password != confirm_password:
print(password)
print(confirm_password)
raise forms.ValidationError(
"Password and password confirmation does not match"
)
return password
self.cleaned_data.get('password')
returns the typed password but the confirm_password doesnt.
In the view.py
when I see the received data before cleaning, the confirm_password shows as it is
......................user = UserRegister(request.POST)
print(request.POST.get('confirm_password'))
if user.is_valid():
print('valid')..........................
What could possibly be the reason for this??
Heres the form declaration part in forms.py
first_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'First Name'}))
last_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'Last Name'}))
username = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'Username'}))
email = forms.EmailField( required=True, widget=forms.widgets.EmailInput(attrs={'placeholder': 'Email'}))
password =forms.CharField(required=True, widget=forms.widgets.PasswordInput())
confirm_password =forms.CharField(required=True, widget=forms.widgets.PasswordInput())
class Meta:
model=User
fields=['first_name','last_name','username','email','password','confirm_password']
回答1:
clean_fieldname
methods called in order of fields declaration, so at the moment clean_password
is called cleaned_data
does not contain confirm_password
. You can perform this validation in clean_confirm_password
method:
def clean_confirm_password(self):
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if password != confirm_password:
print(password)
print(confirm_password)
raise forms.ValidationError(
"Password and password confirmation does not match"
)
return password
or just use clean
method for validation that requires access to multiple form fields.
回答2:
The clean_<fieldname>()
methods are called as the fields are being validated. You can't count on any other fields being in cleaned_data at that time expect for the field you are working with. If you need to work with multiple fields override the form's clean() method.
回答3:
Since you do the validation between multiple fields you should override form's clean
method.
def clean(self):
cleaned_data = super().clean()
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if password != confirm_password:
print(password)
print(confirm_password)
raise forms.ValidationError(
"Password and password confirmation does not match"
)
return cleaned_data
来源:https://stackoverflow.com/questions/49958248/django-cleaned-data-getobj-method-returns-none