Django modelform how to add a confirm password field?

前端 未结 4 698
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 01:40

Here I need to add an extra confirmation password in my form.I used Django\'s modelform. I also need to validate both passwords. It must raise a validation error if

4条回答
  •  面向向阳花
    2021-02-04 01:59

    Use clean like

    class UserForm(forms.ModelForm):
        password=forms.CharField(widget=forms.PasswordInput())
        confirm_password=forms.CharField(widget=forms.PasswordInput())
        class Meta:
            model=User
            fields=('username','email','password')
    
        def clean(self):
            cleaned_data = super(UserForm, self).clean()
            password = cleaned_data.get("password")
            confirm_password = cleaned_data.get("confirm_password")
    
            if password != confirm_password:
                raise forms.ValidationError(
                    "password and confirm_password does not match"
                )
    

提交回复
热议问题