2017-06-07 78 views
0

我是新的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

由於您試圖獲取數據庫中不存在的對象,您會收到錯誤消息。你沒有張貼回溯,但我認爲這就是問題所在:

try: 
    Prospect.objects.get(email=email) 
    raise forms.ValidationError('This email is taken') 
except VendicsUser.DoesNotExist: 
    return email 

get()提出了Prospect.DoesNotExist例外,而不是VendicsUser.DoesNotExist。上升的例外通過您的except區塊。更新所有代碼以反映你想要捕捉的異常,並且你很好。

+0

我沒有看到。我忘了更新例外。謝謝。它現在有效。 –