Django modelform how to add a confirm password field?

前端 未结 4 675
没有蜡笔的小新
没有蜡笔的小新 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 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')
    

提交回复
热议问题