简单例子:
1 #自定义验证类 2 class Check_form1(forms.Form): 3 #user就是要验证的字段,这里对应前端name <input type='text' name=user> 4 user = fields.CharField() 5 pwd = fields.CharField() 6 email = fields.EmailField() 7 8 9 10 def test_form1(request): 11 if request.method == 'GET': 12 return render(request,'form1.html') 13 14 elif request.method == 'POST': 15 obj = Check_form(request.POST) 16 17 #obj.is_valid()表单验证,全通过返回True,有错误就false 18 result = obj.is_valid() 19 if result: 20 21 #{'pwd': u'12311', 'user': u'12311', 'email': u'123@11.com'} 22 print(obj.cleaned_data) 23 else: 24 #打印错误字段 25 #如:<ul class="errorlist"><li>pwd<ul class="errorlist"><li>最小4位</li></ul></li></ul> 26 #返回json格式错误:obj.errors.as_json() 27 # {"pwd": [{"message": "u6700u5c0f4u4f4d", "code": "min_length"}]} 28 print(obj.errors) 29 return HttpResponse("ok")
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <form action="/test_form1/"method="post"> 10 {% csrf_token %} 11 <p> <input type="text" name="user"></p> 12 <p> <input type="text" name="pwd"></p> 13 <p> <input type="text" name="email"></p> 14 <input type="submit" value="提交" /> 15 </form> 16 17 18 </body> 19 </html>
阶级例子:
1 #自定义验证类 2 class Check_form1(forms.Form): 3 user = fields.CharField() 4 pwd = fields.CharField() 5 email = fields.EmailField() 6 7 class Check_form(forms.Form): 8 9 #user就是要验证的字段,这里对应前端name <input type='text' name=user>,django中在前端也可以不写原生格式 10 #widget=widgets.Textarea格式插件,默认text改成了Textarea 11 user = fields.CharField( 12 error_messages={'required':'用户名不能为空'}, 13 widget=widgets.Textarea(attrs={'class':'a1','divclass':'xxxxx'}) 14 ) 15 #max_length大于12,min_length小于4 16 #error_messages错误信息默认是英文的,'required'就是为空,'max_length'字段超过12的,'min_length'字段小于4 17 pwd = fields.CharField( 18 max_length=12, 19 min_length=4, 20 error_messages={'required':'密码不能为空','max_length':'最大12位','min_length':'最小4位'}, 21 widget=widgets.PasswordInput 22 ) 23 24 #'invalid'格式错误 25 email = fields.EmailField( 26 error_messages={'required':'邮箱不能为空','invalid':'邮箱格式错误'}, 27 widget=widgets.EmailInput 28 ) 29 30 31 def test_form(request): 32 if request.method == 'GET': 33 #get方式求情这里的类就不需要传递参数了 34 obj = Check_form() 35 return render(request,'form.html',{'obj':obj}) 36 37 elif request.method == 'POST': 38 #获取用户所有数据 39 #每条数据请求的验证 40 #成功或失败都显示信息 41 42 #将post的数据传给Check_form类 43 obj = Check_form(request.POST) 44 #验证表单数据 45 result = obj.is_valid() 46 47 if result: 48 #obj.cleaned_data这个格式{'pwd': u'12345', 'user': u'123', 'email': u'asd@qq.com'} 49 #这样就可以直接存储到数据库了xxx(**obj.cleaned_data) 50 print(obj.cleaned_data) 51 else: 52 #错误格式<ul class="errorlist"><li>pwd<ul class="errorlist"><li>最小4位</li></ul></li><li>email<ul class="errorlist"><li>邮箱不能为空</li></ul></li></ul> 53 print(obj.errors) 54 55 #json格式输出错误 56 #print(obj.errors.as_json()) 57 58 #打印user字段的具体错误 59 #print(obj.errors['user'][0]) 60 61 return render(request,'form.html',{'obj':obj}) 62 return render(request,'form.html',{'obj':obj})
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <form action="/test_form/"method="post"> 10 {% csrf_token %} 11 <p> {{ obj.user }} {{ obj.errors.user.0 }}</p> 12 <p> {{ obj.pwd }} {{ obj.errors.pwd.0 }}</p> 13 <p> {{ obj.email }} {{ obj.errors.email.0 }}</p> 14 <input type="submit" value="提交" /> 15 </form> 16 17 18 <h1>obj.as_table方式</h1> 19 <form action="/test_form/"method="post"> 20 {% csrf_token %} 21 <!-- 必须自己写table --> 22 <table> 23 {{ obj.as_table }} 24 </table> 25 </form> 26 27 28 <h1>obj.as_p方式</h1> 29 <form action="/test_form/"method="post"> 30 {% csrf_token %} 31 {{ obj.as_p }} 32 </form> 33 34 35 <h1>obj.as_ul方式</h1> 36 <form action="/test_form/"method="post"> 37 {% csrf_token %} 38 {{ obj.as_ul }} 39 </form> 40 41 </body> 42 </html>