2016-12-29 61 views
0

我有問題在我的表單中呈現錯誤,當表單無效時,它只是重新加載頁面,錯誤不顯示。 我想要顯示的錯誤,如顯示說,電子郵件是無效的,或者說,電話號碼包含無效字符Django表單沒有呈現錯誤

這裏是我的代碼的文本:

views.py

def contact(request): 
    form_class = ContactForm 

    if request.method == 'POST': 
     form = form_class(data=request.POST) 

     if form.is_valid(): 
      contact_name = request.POST.get(
       'contact_name' 
       , '') 
      contact_email = request.POST.get(
       'contact_email' 
       , '') 
      contact_phone = request.POST.get(
       'contact_phone' 
       , '') 
      form_content = request.POST.get(
       'content' 
       , '') 

      # Email the profile with the 
      # contact information 
      template = get_template('contact_form.txt') 
      context = Context({ 
       'contact_name': contact_name, 
       'contact_email': contact_email, 
       'contact_phone': contact_phone, 
       'form_content': form_content, 
      }) 
      content = template.render(context) 

      email = EmailMessage(
       "Novo contato pelo site", 
       content, 
       "[email protected]", 
       ['[email protected]'], 
       headers={'Reply-To': contact_email} 
      ) 
      email.send() 

      print(form.cleaned_data) 
     else: 
      print(form) 

    return render(request, 'info/contact_us.html', { 
     'form': form_class, 
    }) 

forms.py

class ContactForm(forms.Form): 
    contact_name = forms.CharField(max_length=150, 
            label="Nome", 
            required=True,) 
    contact_email = forms.EmailField(max_length=150, 
            label="Email", 
            required=True,) 
    contact_phone = forms.RegexField(max_length=12, 
            label="Fone", 
            required=False, 
            regex=r'[0-9]+',) 
    content = forms.CharField(max_length=10000, 
           required=True, 
           widget=forms.Textarea,) 

    def __init__(self, *args, **kwargs): 
     super(ContactForm, self).__init__(*args, **kwargs) 
     self.helper = FormHelper(self) 
     self.helper.form_id = 'id-form' 
     self.helper.form_class = 'blueForms' 
     self.helper.form_method = 'post' 
     self.helper.form_action = '' 

     self.helper.add_input(Submit('submit', 'Submit')) 

     self.fields['contact_name'].label = "Name:" 
     self.fields['contact_email'].label = "Email:" 
     self.fields['contact_phone'].label = "Phone:" 
     self.fields['content'].label = "Message:" 

    def clean(self): 
     cleaned_data = super(ContactForm, self).clean() 
     contact_name = cleaned_data.get("contact_name") 
     contact_email = cleaned_data.get("contact_email") 
     contact_phone = cleaned_data.get("contact_phone") 
     content = cleaned_data.get("content") 

     if 'asd' not in contact_email: 
      raise forms.ValidationError("Invalid Email") 

contact_us.html

<div id="form" class="col-sm-6"> 
    {% crispy form form.helper %} 
</div> 
+0

凡的這一觀點的休息嗎?目前你會得到該代碼的服務器錯誤。 –

+0

糟糕,剛剛添加了我忘記的最後一部分。 –

回答

1

錯誤是在這條線

return render(request, 'info/contact_us.html', { 
    'form': form_class, 
}) 

當GET方法被調用它加載空形式是形式= form_class(),關於POST方法應該是形成= form_class(request.POST)。按照上面的代碼,它再次裝載新鮮的形式

添加您return語句的POST塊內部也

return render(request, 'info/contact_us.html', { 
    'form': form_class(request.POST), 
}) 

return render(request, 'info/contact_us.html', { 
    'form': form, 
}) 
+0

'form_class(request.POST)'可以工作,但是在表單上寫任何東西之前它會顯示錯誤 –

+0

剛剛解決了這個問題,我不得不在'if_valid()'末尾加上'return render form_class(request.POST) '並在代碼的末尾使用'return render form_class' –