首页 > 代码库 > django下简单表单验证实现上
django下简单表单验证实现上
from django import forms class ContactForm(forms.Form): subject = forms.CharField() email = forms.EmailField(required=False) message = forms.CharField()
from django.shortcuts import render_to_response from mysite.contact.forms import ContactForm def contact(request): if request.method == ‘POST‘: form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data send_mail( cd[‘subject‘], cd[‘message‘], cd.get(‘email‘, ‘noreply@example.com‘), [‘siteowner@example.com‘], ) return HttpResponseRedirect(‘/contact/thanks/‘) else: form = ContactForm() #必须加最后一句否则或出错return render_to_response(‘contact/contact_form.html‘, {‘form‘: form},context_instance=RequestContext(request))
contact_form.html
<html> <head> <title>Contact us</title> </head> <body> <h1>Contact us</h1> {% if form.errors %} <p style="color: red;"> Please correct the error{{ form.errors|pluralize }} below. </p> {% endif %} <form action="" method="post"> {% csrf_token %} <table> {{ form.as_table }} </table> <input type="submit" value=http://www.mamicode.com/"Submit">>更多关于android开发 的文章django下简单表单验证实现上
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。