1 form对象.errors 字典
2 name对象.errors
## 视图函数
def register(request):
if request.method=='GET':
form=myforms.MyForm()
return render(request, 'register.html',{'form':form})
else:
form=myforms.MyForm(request.POST)
if form.is_valid():
return redirect('http://www.baidu.com')
else:
return render(request, 'register.html',{'form':form})
## 模板
<form action="" method="post" novalidate>
{% for foo in form %}
<div class="form-group">
<label for="">{{ foo.label }}</label>
{{ foo }}
<span class="text-danger pull-right">{{ foo.errors }}</span>
</div>
{% endfor %}
<div class="text-center">
<input type="submit" value="提交" class="btn btn-danger">
</div>
</form>
2 forms组件参数配置
# 定制模板中的显示样式,及配置类
# widget=widgets.PasswordInput(attrs={'class': 'form-control'})
# 错误信息中文显示
error_messages={'min_length': '太短了小伙子'}
class MyForm(forms.Form):
# 校验这个字段,最大长度是32,最小长度是3
name = forms.CharField(required=False, max_length=32, min_length=3, label='用户名',
widget=widgets.TextInput(attrs={'class': 'form-control'}),
error_messages={'min_length': '太短了小伙子'})
password = forms.CharField(required=False, max_length=32, min_length=3, label='密码',
widget=widgets.PasswordInput(attrs={'class': 'form-control'}),
error_messages={'min_length': '太短了小伙子'})
re_password = forms.CharField(required=False, max_length=32, min_length=3, label='确认密码',
widget=widgets.PasswordInput(attrs={'class': 'form-control'}),
error_messages={'min_length': '太短了小伙子'})
email = forms.EmailField(label='邮箱', error_messages={'required': '小惠子,这个必填'},
widget=widgets.TextInput(attrs={'class': 'form-control'}))
age = forms.IntegerField(max_value=200, min_value=0, label='年龄',
widget=widgets.TextInput(attrs={'class': 'form-control'}))
text = forms.CharField(label='个人简介', widget=widgets.Textarea(attrs={'class': 'form-control'}))
date = forms.CharField(label='出生日期', widget=widgets.DateInput(attrs={'class': 'form-control'}))
3 局部钩子和全局钩子
## 局部钩子的使用
# 1 在自定义的Form类中写 clean_字段名
# 2 取出字段的真正值,name=self.cleaned_data.get('name')
# 3 判断自己的规则,如果判断失败,抛出ValidationError
# 4 如果通过,return name
# 局部钩子
def clean_name(self):
# name对应的值,如何取到?
name = self.cleaned_data.get('name')
if name.startswith('sb'):
# 不让校验通过
raise ValidationError('不能以sb开头')
else:
# 校验通过,返回name
return name
# 全局钩子
def clean(self):
# name=self.cleaned_data.get('name')
# print(name)
password = self.cleaned_data.get('password')
re_password = self.cleaned_data.get('re_password')
if password == re_password:
return self.cleaned_data
# return {'lqz':"nb"}
else:
raise ValidationError('两次密码不一致')
4 cookie session token
1 https://www.cnblogs.com/liuqingzheng/articles/8990027.htmlp
2 cookie:客户端浏览器上的键值对
3 session:存在服务端的键值对
4 token:加密的键值对,如果放在客户端浏览器上,它就叫cookie, 服务端签发的加密字符串
head.{name:lqz,age:18}.eseetsweasdca
base64加码:
asdfasfd.asdfasdf.asdfasdfaeraew
后端校验:
用这个token去查我的账户余额,向银行发请求,银行校验通过,是银行给你的,---》返回你的余额
head.{name:lqz,age:18}.eseetsweasdca
head.{name:egon,age:18}.eseetsweasdca
def cookie_test(request):
# 浏览器向我这个地址发一个请求,就在浏览器写入 name = lqz
obj=HttpResponse('ok')
obj.set_cookie('name','egon') # 写入到浏览器了,在http响应头里:cookie: name=lqz
obj.set_cookie('age','19') # 写入到浏览器了,在http响应头里:cookie: name=lqz
return obj
def get_cookie(request):
print(request.COOKIES)
print(request.COOKIES.get('name'))
return HttpResponse('我拿了你传过来的cookie')
def delete_cookie(request):
obj=HttpResponse('我删掉了你 name 这个cookie ')
obj.delete_cookie('name')
return obj