Django ::未显示验证错误

时间:2019-03-01 18:04:39

标签: python html django

最近我正在使用Django进行验证。但是当我输入错误的输入时,我并没有收到验证错误。

请提供一些我做错地方的建议。

下面是我的python和html示例代码,

  

Views.py

def contact_page(request):

contact_form = ContactForm(request.POST or None)
context = {
    "title":"contact page",
    "content":"Welcome to Contact page",
    "form": contact_form
}

if contact_form.is_valid():
    print(contact_form.cleaned_data)

return render(request, "contact/view.html", context)
  

Forms.py //使用Django表单创建表单

from django import forms
// creating a form having 3 fields fullname, email and content (for some message )

class ContactForm(forms.Form):
fullname = forms.CharField(
            widget=forms.TextInput(
                attrs={
                    "class":"form-control",
                    "placeholder" :"Your name "
                    }
                )
            )

email    = forms.EmailField(
            widget=forms.EmailInput(
                attrs={
                    "class":"form-control",
                    "placeholder" :"Your Email "
                    }
                )
            )
content  = forms.CharField(
            widget=forms.Textarea(
                attrs={
                    "class":"form-control",
                    "placeholder":"Your text"
                    }
                )
            )

// form validation 

def clean_email(self):      
    email = self.cleaned_data.get("email")
    if not "gmail.com" in email:
        raise forms.ValidationError("Email has to be gmail.com")
    return email

enter image description here

0 个答案:

没有答案