python+django实现的简单的表单验证源码

下面的内容段是关于python+django实现的简单的表单验证的内容,应该能对大伙也有用途。

创新互联是一家专业提供揭阳企业网站建设,专注与做网站、成都网站设计、html5、小程序制作等业务。10年已为揭阳众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。



  Form validation example
  



  

Form validation example

{% if form.errors %}
Please correct the error{{ form.errors|pluralize }} below.
{% endif %}

{% csrf_token %}
{{ form.subject }} {% if form.subject.errors %} {% for error in form.subject.errors %}{{ error }}{% endfor %} {% endif %}
{{ form.email }} {% if form.email.errors %} {% for error in form.email.errors %}{{ error }}{% endfor %} {% endif %}
{{ form.message }} {% if form.message.errors %} {% for error in form.message.errors %}{{ error }}{% endfor %} {% 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)

本文题目:python+django实现的简单的表单验证源码
文章分享:http://azwzsj.com/article/poichp.html