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

2020-12-13 01:47

阅读:583

YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

标签:ext   tst   return   sheet   lse   BMI   method   field   textinput   

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

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实现的简单的表单验证源码

标签:ext   tst   return   sheet   lse   BMI   method   field   textinput   

原文地址:https://blog.51cto.com/14392820/2407783


评论


亲,登录后才可以留言!