全局钩子验证:
‘’’ 打包前端input,views数据处理,链接moduls数据库,用来验证 ’’’
Views:
Form=UserForm(request.POST)实例化对象
Form.cleaned_data #干净字段和值
Form.errors #dict---{‘效验错误字段’:’错误信息’}----form.errors.get(‘效验错误字段’)
form.errors.get(‘__all__’) #全局钩子错误信息
UserForm类定义:
From django import forms #自动验证
From django.forms import widgets
From django.core.exception import ValidationError
from django.core.validators import RegexValidator #正则
Wid_02 = widgets.PasswordInput(attrs={‘class’:’form-control’})#形成密码输入框
Pwd = forms.CharField(max_length=6,min_length=2,widget=wid_02,label=’密码’,validators=[RegexValidator(‘d+’,’只能是数字’)],error_messages={‘required’:’该字段必填’'min_length': '密码长度不能小于8','max_length': '密码长度不能大于18','invalid': '密码格式错误',}) #对输入框进行限制
#局部钩子---判断输入是否在数据库中
Def clean_member_name:
Val = self.cleaned_data.get(‘username’)
Res = Member.objects.filter(username=val)
If判断res:
Return val
Else:
Raise ValidationError(‘用户名已存在’)
#全局钩子(前端和前端)
Def xxx:
密码==重复密码
Html:
Form表单里:
1:
<lable for=’pwd’>{{form.username.lable}}</lable> //标签名
<p>{{form.username}}<span class=’’pull-right error’’>{{form.username.errors.0}}</span></p>
//输入框----错误信息
2:
<lable for=’pwd’>{{form.username.lable}}</lable> //标签名
<p><input type=’password’ name=’pwd’ value={{ request.POST.pwd }}<span class=’’pull-right error’’>{{form.username.errors.0}}</span></p>
// request.POST.pwd == request.POST.get(‘pwd’)
3:
如果不想加error_message来显示中文信息,可以在setting里修改配置
LANGUAGE_CODE = ‘zh-hans’
这样错误信息自动转为中文
my_form:
from django import forms #自动验证 from django.forms import widgets from django.core.exceptions import ValidationError from app01.models import Member class UserForm(forms.Form): '''''' wid_email = widgets.EmailInput(attrs={'class':'form - control'}) wid_phone = widgets.TextInput(attrs={'class':'form - control'}) wid_username = widgets.TextInput(attrs={'class':'form - control'}) wid_displyname = widgets.TextInput(attrs={'class':'form - control'}) wid_password = widgets.PasswordInput(attrs={'class':'form - control'}) wid_rpassword = widgets.PasswordInput(attrs={'class':'form - control'}) member_email = forms.EmailField(max_length=120, min_length=2, widget=wid_email, label='邮箱',error_messages = {'required':'该字段必填'}) member_tel = forms.CharField(max_length=13, min_length=4, widget=wid_phone, label='手机号',error_messages = {'required':'该字段必填'}) member_name = forms.CharField(max_length=30, min_length=2, widget=wid_username, label='用户名',error_messages = {'required':'该字段必填'}) member_nickname = forms.CharField(max_length=20, min_length=2, widget=wid_displyname, label='昵称',error_messages = {'required':'该字段必填'}) member_pwd = forms.CharField(max_length=30, min_length=8, widget=wid_password, label='密码',error_messages = {'required':'该字段必填'}) r_pwd = forms.CharField(max_length=30, min_length=8, widget=wid_rpassword, label='重复密码',error_messages = {'required':'该字段必填'}) def clean_member_name(self): '''''' val = self.cleaned_data.get('member_name') res = Member.objects.filter(member_name=val) print(res) if not res: return val else: return ValidationError('用户名已存在!') def clean(self): '''''' member_pwd = self.cleaned_data.get('member_pwd') r_pwd = self.cleaned_data.get('r_pwd') if member_pwd and r_pwd: if member_pwd == r_pwd: return self.cleaned_data else: return ValidationError('两次密码不一致!') else: return self.cleaned_data
views:
from django.shortcuts import render,HttpResponse,redirect from app01.models import * from app01.my_form import UserForm import json def enroll(request): '''''' res = {'s':None,'info':None} print(request.POST) if request.method == 'POST': form = UserForm(request.POST) print(form) print(form.errors) print(1) if not form.is_valid(): res['s'] = 0 res['info'] = form.errors print(2) return HttpResponse(json.dumps(res)) email = request.POST.get('member_email') print(email) phone = request.POST.get('member_tel') username = request.POST.get('member_name') displyname = request.POST.get('member_nickname') password = request.POST.get('member_pwd') member_obj = Member.objects.create(member_email=email,member_tel=phone,member_name=username,member_nickname=displyname,member_pwd=password) print(3) if member_obj: res['s'] = 1 res['info'] = '注册成功' else: res['s'] = 2 res['info'] = '注册失败' response = HttpResponse(json.dumps(res)) request.session['username'] = member_obj.member_name request.session['member_id'] = member_obj.id print(4) return response return render(request, 'app01_enroll.html', locals())