我是django的新手,我遇到了这种形式问题。我不知道这个错误意味着什么。
潜在客户匹配查询不存在。
它说错误发生在这里:如果form.is_valid():
这是我的档案:
forms.py
class ProspectForm(forms.ModelForm):
full_name = forms.CharField(
label="Full Name",
max_length=120,
required=False,
)
email = forms.EmailField(
label="E-mail",
max_length=120,
required=False,
)
contact_number = forms.CharField(
label="Contact Number",
max_length=14,
required=False,
)
def clean_email(self):
email = self.cleaned_data.get('email')
try:
Prospect.objects.get(email=email)
raise forms.ValidationError('This email is taken')
except VendicsUser.DoesNotExist:
return email
def clean_number(self):
number = self.cleaned_data.get('number')
validate_integer(number)
try:
Prospect.objects.get(contact_number=number)
raise forms.ValidationError('This number is taken')
except VendicsUser.DoesNotExist:
if len(number) < 11:
return number
else:
raise forms.ValidationError('Number is too short')
def __init__(self, *args):
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.form_class = 'contact_form'
self.helper.form_action = ''
self.helper.layout = Layout(
Field('full_name', css_class='form_fields'),
Field('email', css_class='form_fields'),
Field('contact_number', css_class='form_fields'),
Hidden('sub_domain', ''),
FormActions(
Submit('submit', 'SUBMIT', css_class='form_button'),
)
)
super(ProspectForm, self).__init__(*args)
class Meta:
model = Prospect
exclude = ['user']
views.py
def landingpage(request, subdomain=None):
form = ProspectForm(request.POST or None)
if form.is_valid():
form.save(commit=False)
# for future changes
form.save()
return redirect('' + subdomain + '/')
context = {
'subdomain': subdomain,
'form': form,
}
user = get_object_or_404(WebDetail, domain=subdomain)
return render(request, "landingpage/landingpage.html", context)
答案 0 :(得分:0)
您收到错误是因为您试图获取数据库中不存在的对象。你没有发布回溯,但我认为这是问题所在:
try:
Prospect.objects.get(email=email)
raise forms.ValidationError('This email is taken')
except VendicsUser.DoesNotExist:
return email
get()
引发Prospect.DoesNotExist
例外,而不是VendicsUser.DoesNotExist
。引发的异常会超过except
块。更新所有代码,以反映您尝试捕获的异常,并且您可以继续使用。