Django组件表格组件
Django组件表格组件
forms组件
校验字段功能
针对一个实例:注册用户讲解。
模型:模型py .
类用户信息(模型。型号):
名称=型号。字符字段(最大长度=32)
pwd=模型。字符字段(最大长度=32)
电子邮件=模型EmailField()
电话=型号。字符字段(最大长度=32)
模板: register.html:
!DOCTYPE html
html lang='en '
头
meta charset='UTF-8 '
标题标题/标题
/head
身体
表单操作='方法='发布'
{% csrf_token %}
差异
"="用户的标签用户名/标签
pinput类型='text' name='name' /p
/div
差异
='pwd '的标签密码/标签
pinput类型='password' name='pwd' /p
/div
差异
='r_pwd '的标签确认密码/标签
pinput类型='password' name='r_pwd' /p
/div
差异
='电子邮件'的标签邮箱/标签
pinput type=' text ' name=' email '/p
/div
输入类型="提交"
/form
/body
/html
视图函数:注册
#表单组件
从django.forms导入小部件
wid _ 01=小部件TextInput(attrs={ ' class ' : ' form-control ' })
wid _ 02=小部件密码输入(attrs={ ' class ' : ' form-control ' })
类用户表单(表单。表格):
名称=表格.CharField(最大长度=32,
widget=wid_01
)
pwd=表单CharField(max_length=32,widget=wid_02)
r _ pwd=表单CharField(max_length=32,widget=wid_02)
电子邮件=表单EmailField(widget=wid_01)
电话=表格.CharField(max_length=32,widget=wid_01)
极好的寄存器(请求):
if request.method=='POST':
表单=用户表单(请求。开机自检)
if form.is_valid():
打印(表单。已清理_数据)#所有干净的字段以及对应的值
else:
打印(表单。已清理_数据)#
打印(表格。错误)#错误字典: { '校验错误的字段':['错误信息',]}
打印(表格。错误。get(' name ')#错误列表['错误信息',]
返回"确定"
表单=用户表单()
返回渲染(请求、‘register.html’,本地人())
渲染标签功能
渲染方式1
!DOCTYPE html
html lang='en '
头
meta charset='UTF-8 '
标题标题/标题
!-最新版本的引导程序核心半铸钢钢性铸铁(铸造半钢)文
件 --
link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"
/head
body
h3注册页面/h3
div class="container"
div class="row"
div class="col-md-6 col-lg-offset-3"
form action="" method="post"
{% csrf_token %}
div
label for=""用户名/label
{{ form.name }}
/div
div
label for=""密码/label
{{ form.pwd }}
/div
div
label for=""确认密码/label
{{ form.r_pwd }}
/div
div
label for="" 邮箱/label
{{ form.email }}
/div
input type="submit" class="btn btn-default pull-right"
/form
/div
/div
/div
/body
/html
渲染方式2
form action="" method="post" {% csrf_token %} {% for field in form %} div label for=""{{ field.label }}/label {{ field }} /div {% endfor %} input type="submit" class="btn btn-default pull-right" /form
渲染方式3
form action="" method="post" {% csrf_token %} {{ form.as_p }} input type="submit" class="btn btn-default pull-right" /form
显示错误与重置输入信息功能
视图
def register(request): if request.method=="POST": form=UserForm(request.POST) if form.is_valid(): print(form.cleaned_data) # 所有干净的字段以及对应的值 else: print(form.cleaned_data) # print(form.errors) # ErrorDict : {"校验错误的字段":["错误信息",]} print(form.errors.get("name")) # ErrorList ["错误信息",] return render(request,"register.html",locals()) form=UserForm() return render(request,"register.html",locals())
模板
form action="" method="post" novalidate {% csrf_token %} {% for field in form %} div label for=""{{ field.label }}/label {{ field }} span class="pull-right" style="color: red"{{ field.errors.0 }}/span /div {% endfor %} input type="submit" class="btn btn-default" /form
局部钩子与全局钩子
模板
# forms组件 from django.forms import widgets wid_01=widgets.TextInput(attrs={"class":"form-control"}) wid_02=widgets.PasswordInput(attrs={"class":"form-control"}) from django.core.exceptions import ValidationError class UserForm(forms.Form): name=forms.CharField(max_length=32, widget=wid_01 ) pwd=forms.CharField(max_length=32,widget=wid_02) r_pwd=forms.CharField(max_length=32,widget=wid_02) email=forms.EmailField(widget=wid_01) tel=forms.CharField(max_length=32,widget=wid_01) # 局部钩子 def clean_name(self): val=self.cleaned_data.get("name") if not val.isdigit(): return val else: raise ValidationError("用户名不能是纯数字!") # 全局钩子 def clean(self): pwd=self.cleaned_data.get("pwd") r_pwd=self.cleaned_data.get("r_pwd") if pwd==r_pwd: return self.cleaned_data else: raise ValidationError('两次密码不一致!') def register(request): if request.method=="POST": form=UserForm(request.POST) if form.is_valid(): print(form.cleaned_data) # 所有干净的字段以及对应的值 else: clean_error=form.errors.get("__all__") return render(request,"register.html",locals()) form=UserForm() return render(request,"register.html",locals())
视图
form action="" method="post" novalidate {% csrf_token %} {% for field in form %} div label for=""{{ field.label }}/label {{ field }} span class="pull-right" style="color: red" {% if field.label == 'R pwd' %} span{{ clean_error.0 }}/span {% endif %} {{ field.errors.0 }} /span /div {% endfor %} input type="submit" class="btn btn-default" /form
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/143777.html