在表单中创建 mixin 以检查密码
Create mixin in forms to check passwords
我有一些 Patient、Doctors 等模型,然后我使用它们创建基于 ModelForm 的表单,其中包含一些附加字段:
class CreateClinicForm(forms.ModelForm):
email = forms.EmailField(required=True)
name = forms.CharField(max_length=200, required=True)
specialties = forms.CharField(max_length=100, required=True)
phone = forms.IntegerField(required=True)
class Meta:
model = Clinic
fields = ('name', 'specialties')
而且我在这些表单中进行了密码检查,所以使用 mixin 会很好。
我试过创建这个,但是没有用。
class PasswordMatchMixin(object):
password = forms.CharField(max_length=32, required=True)
password_confirm = forms.CharField(max_length=32, required=True)
def clean_password_confirm(self):
password = self.cleaned_data.get('password')
password_confirm = self.cleaned_data.get('password_confirm')
if password and password != password_confirm:
raise ValidationError(_("Passwords don't match"))
return self.password_confirm
当然,之后我像这样将它添加到我的表格中:
class CreateDoctorForm(PasswordMatchMixin, forms.ModelForm) : ...
有什么想法或想法?
如果您是 cleaning fields that rely on each other,您应该覆盖 clean
而不是 clean_password
。
class PasswordMatchMixin(object):
password = forms.CharField(max_length=32, required=True)
password_confirm = forms.CharField(max_length=32, required=True)
def clean(self):
cleaned_data = super(PasswordMatchMixin, self).clean()
password = cleaned_data.get('password')
password_confirm = cleaned_data.get('password_confirm')
if password and password != password_confirm:
raise ValidationError(_("Passwords don't match"))
return cleaned_data
如果您在表单中覆盖 clean
,请记住调用 super()
以便调用 PasswordMatchMixin.clean
方法。
我有一些 Patient、Doctors 等模型,然后我使用它们创建基于 ModelForm 的表单,其中包含一些附加字段:
class CreateClinicForm(forms.ModelForm):
email = forms.EmailField(required=True)
name = forms.CharField(max_length=200, required=True)
specialties = forms.CharField(max_length=100, required=True)
phone = forms.IntegerField(required=True)
class Meta:
model = Clinic
fields = ('name', 'specialties')
而且我在这些表单中进行了密码检查,所以使用 mixin 会很好。 我试过创建这个,但是没有用。
class PasswordMatchMixin(object):
password = forms.CharField(max_length=32, required=True)
password_confirm = forms.CharField(max_length=32, required=True)
def clean_password_confirm(self):
password = self.cleaned_data.get('password')
password_confirm = self.cleaned_data.get('password_confirm')
if password and password != password_confirm:
raise ValidationError(_("Passwords don't match"))
return self.password_confirm
当然,之后我像这样将它添加到我的表格中:
class CreateDoctorForm(PasswordMatchMixin, forms.ModelForm) : ...
有什么想法或想法?
如果您是 cleaning fields that rely on each other,您应该覆盖 clean
而不是 clean_password
。
class PasswordMatchMixin(object):
password = forms.CharField(max_length=32, required=True)
password_confirm = forms.CharField(max_length=32, required=True)
def clean(self):
cleaned_data = super(PasswordMatchMixin, self).clean()
password = cleaned_data.get('password')
password_confirm = cleaned_data.get('password_confirm')
if password and password != password_confirm:
raise ValidationError(_("Passwords don't match"))
return cleaned_data
如果您在表单中覆盖 clean
,请记住调用 super()
以便调用 PasswordMatchMixin.clean
方法。