Django modelform how to add a confirm password field?

前端 未结 4 677
没有蜡笔的小新
没有蜡笔的小新 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"
                )
    
    0 讨论(0)
  • 2021-02-04 02:06
    def clean(self):
        cleaned_data = super(UserAccountForm, self).clean()
        password = cleaned_data.get("password")
        confirm_password = cleaned_data.get("confirm_password")
    
        if password != confirm_password:
            self.add_error('confirm_password', "Password does not match")
    
        return cleaned_data
    
    0 讨论(0)
  • 2021-02-04 02:09

    Try this for forms.py:

    class UserForm(forms.Form):
        password = forms.CharField(widget=forms.PasswordInput())
        password_confirm = forms.CharField(widget=forms.PasswordInput())
    
        class Meta:
            model = User
            fields=('username','email','password')
    

    And this in views.py:

    if user_form.is_valid() and profile_form.is_valid() and user_form.cleaned_data['password'] == user_form.cleaned_data['password_confirm']:
        ...
    elif user_form.data['password'] != user_form.data['password_confirm']:
        user_form.add_error('password_confirm', 'The passwords do not match')
    
    0 讨论(0)
  • 2021-02-04 02:10

    You can have a look at how Django does it for UserCreationForm.

        def clean_password2(self):
            password1 = self.cleaned_data.get("password1")
            password2 = self.cleaned_data.get("password2")
            if password1 and password2 and password1 != password2:
                raise ValidationError(
                    self.error_messages['password_mismatch'],
                    code='password_mismatch',
                )
            return password2
    

    Here password2 refers to the confirm_password field, under the assumption that it appears after the password field. Trying to use the same implementation for clean_password may lead to an error that the confirm_password data wasn't found.

    This has the advantage that you're raising the error for a particular Field, instead of the whole form, which you can then render appropriately in your template.

    However, if you're trying to validate data across multiple fields, the documentation advises overriding the clean() method, as answered by Savai.

    The source code is available here.

    0 讨论(0)
提交回复
热议问题