重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
下面的内容段是关于python+django实现的简单的表单验证的内容,应该能对大伙也有用途。
创新互联是一家专业提供揭阳企业网站建设,专注与做网站、成都网站设计、html5、小程序制作等业务。10年已为揭阳众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
Form validation example
Form validation example
{% if form.errors %}
Please correct the error{{ form.errors|pluralize }} below.
{% endif %}
forms.py
from django import forms
class ContactForm(forms.Form):
"""
define a contact form class
"""
# this will be rendered like
#
# valid if not empty
subject = forms.CharField(widget=forms.TextInput(attrs={'size':'48', 'class':'form-control'}))
# A CharField that checks that the value is a valid email address.
email = forms.EmailField(widget=forms.TextInput(attrs={'size':'48', 'class':'form-control'}))
message = forms.CharField(widget=forms.Textarea(attrs={'cols':50, 'rows': 5 , 'class':'form-control'}))
views.py
from django.shortcuts import render
from .forms import ContactForm
def contact(request):
if request.method == 'POST':
# get data from POST request to contactform
form = ContactForm(request.POST)
else:
form = ContactForm()
data = {
'form': form,
}
return render(request, 'contact_form.html', data)