十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
下面的内容段是关于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)