Django modelform如何添加确认密码字段?

时间:2016-01-05 10:48:22

标签: django-forms password-confirmation

这里我需要在表单中添加一个额外的declare @tbl table(dDay datetime, data int) insert into @tbl values ('2014-11-14 11:03:18',20) insert into @tbl values ('2014-12-17 09:22:03',50) insert into @tbl values ('2014-11-14 10:38:06',35) insert into @tbl values ('2014-12-14 10:41:24',10) insert into @tbl values ('2014-11-14 10:44:53',13) insert into @tbl values ('2014-11-17 09:22:03',11) insert into @tbl values ('2013-03-22 17:46:02',40) insert into @tbl values ('2013-02-22 17:46:02',20) insert into @tbl values ('2013-01-22 17:46:02',10) ;with cte_table as ( select row_number() over (order by datepart(mm,dday)) as rn, datepart(mm,dday) as dmonth, sum(data) data ,case when sum(data) > 50 then 1 else 0 end as flag from @tbl group by datepart(mm,dday) ) select c1.dmonth, c1.data, case when c1.data > 50 then c1.data else ((isnull(c1.data,0)+isnull(c2.data,0))) end as totaldata from cte_table c1 left outer join cte_table c2 on c1.rn = c2.rn+1 。我使用了Django的模型。我还需要验证两个密码。如果confirmation password,则必须提出验证错误。

这是我的forms.py:

password1 != password2

这是我的注册功能:

class UserForm(forms.ModelForm):
    password=forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model=User
        fields=('username','email','password')

class UserProfileForm(forms.ModelForm):
    YESNO_CHOICES = (('male', 'male'), ('female', 'female'))
    sex = forms.TypedChoiceField(choices=YESNO_CHOICES, widget=forms.RadioSelect)
    FAVORITE_COLORS_CHOICES=(('red','red'),('blue','blue'))
    favorite_colors = forms.MultipleChoiceField(required=False,widget=forms.CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)
    dob = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), 
                                 input_formats=('%d/%m/%Y',))

    class Meta:

        model=UserProfile
        fields=('phone','picture','sex','favorite_colors','dob')

4 个答案:

答案 0 :(得分:21)

使用clean之类的

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"
            )

答案 1 :(得分:5)

 z1 = Convolution2D((3,3), 64, pad=True)(input)
 z2 = MaxPooling((2,2), (2,2))(z1)

答案 2 :(得分:1)

尝试使用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')

这在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')

答案 3 :(得分:0)

您可以查看Django如何为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

在此假设password2出现在password字段之后,它是指confirm_password字段。尝试对clean_password使用相同的实现可能会导致错误,导致未找到Confirm_password数据。

这样做的好处是,您要针对特定​​字段而不是整个表单引发错误,然后可以在模板中正确呈现该表单。

但是,如果您要尝试跨多个字段验证数据,则documentation建议覆盖clean()方法,如Savai回答。

源代码可用here