Python 电子邮件验证(1062,密钥和电子邮件的“重复输入和重复输入”)
我在注册中有两个电子邮件字段来验证电子邮件是否正确: 这是我的表格:Python 电子邮件验证(1062,密钥和电子邮件的“重复输入和重复输入”),python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我在注册中有两个电子邮件字段来验证电子邮件是否正确: 这是我的表格: class RegistrationForm(forms.ModelForm): email1 = forms.EmailField(label="El.paštas") email2 = forms.EmailField(label="Pakartokite el. paštą") class Meta: model = AuthUser fields = ("ema
class RegistrationForm(forms.ModelForm):
email1 = forms.EmailField(label="El.paštas")
email2 = forms.EmailField(label="Pakartokite el. paštą")
class Meta:
model = AuthUser
fields = ("email1", "email2", "first_name", "last_name", "password", "city", "other_city", "age", "accepts_emails")
widgets = {
"password": forms.PasswordInput(),
}
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
self.fields["city"].queryset = City.objects.filter(other_city=False)
for field_name in self.fields:
field = self.fields.get(field_name)
if field and field_name != "accepts_emails" and field_name != "other_city":
field.widget.attrs.update({"class": "form-control"})
def clean_email2(self):
email1 = self.cleaned_data["email1"]
email2 = self.cleaned_data["email2"]
if email1 and email2 and email1 != email2:
raise forms.ValidationError("El. pašto adresai nesutampa.")
try:
AuthUser._default_manager.get(email=email1)
except AuthUser.DoesNotExist:
return email1
raise forms.ValidationError("Toks el. paštas jau naudojamas.")
当我试图保存我的模型时,我得到的错误是:
IntegrityError at /registracija/
(1062, "Duplicate entry '' for key 'email'")
如果您还需要其他任何信息,请告知我您应该覆盖
save()
方法,并在此处设置用户的email
字段:
def save(self, *args, **kwargs):
user = super(RegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email1']
user.save()
return user
“重复输入”错误是因为您在
电子邮件中有唯一的密钥。但是,您的字段是email1
和email2
。你迁移了应用程序吗?什么是迁移?我是指你的应用程序的南迁移,以便更新数据库中的模型AuthUser。你的AuthUser
模型是什么?