Django之Form组件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django之Form组件相关的知识,希望对你有一定的参考价值。
一、Form组件介绍1、Form组件可以做的几件事情
1、用户请求数据验证
2、自动生成错误信息
3、打包用户提交的正确信息
4、如果其中有一个错误了,其他的正确这,保留上次输入的内容
5、自动创建input标签并可以设置样式
2、Django内置字段如下
Field required = True, #是否允许为空 widget = None, #html插件 label = None, #用于生成Label标签或显示内容 initial = None, #初始值 help_text = '', #帮助信息(在标签旁边显示) error_messages = None, #错误信息{'required': '不能为空', 'invalid': '格式错误'} show_hidden_initial = False, #是否在当前插件后面再加一个隐藏的且具有默认值的插件(可用于检验两次输入是否一直) validators = [], #自定义验证规则 localize = False, #是否支持本地化 disabled = False, #是否可以编辑 label_suffix = None #Label内容后缀 CharField(Field) max_length = None, #最大长度 min_length = None, #最小长度 strip = True #是否移除用户输入空白 IntegerField(Field) max_value = None, #最大值 min_value = None, #最小值 DecimalField(IntegerField) max_value = None, #最大值 min_value = None, #最小值 max_digits = None, #总长度 decimal_places = None, #小数位长度 BaseTemporalField(Field) input_formats = None # 时间格式化 DateField(BaseTemporalField) #格式:2015 - 09 - 01 TimeField(BaseTemporalField) # 格式:11: 12 DateTimeField(BaseTemporalField) #格式:2015 - 09 - 01 11: 12 DurationField(Field) #时间间隔: % d % H: % M: % S. % f ... RegexField(CharField) regex, #自定制正则表达式 max_length = None, #最大长度 min_length = None, #最小长度 error_message = None, #忽略,错误信息使用error_messages = {'invalid': '...'} FileField(Field) allow_empty_file = False #是否允许空文件 ChoiceField(Field) ... choices = (), #选项,如:choices = ((0, '上海'), (1, '北京'),) required = True, #是否必填 widget = None, #插件,默认select插件 label = None, #Label内容 initial = None, #初始值 help_text = '', #帮助提示 ModelChoiceField(ChoiceField) ... django.forms.models.ModelChoiceField queryset, # 查询数据库中的数据 empty_label = "---------", # 默认空显示内容 to_field_name = None, # HTML中value的值对应的字段 limit_choices_to = None # ModelForm中对queryset二次筛选 ModelMultipleChoiceField(ModelChoiceField) ... django.forms.models.ModelMultipleChoiceField TypedChoiceField(ChoiceField) coerce = lambda val: val #对选中的值进行一次转换 empty_value = '' # 空值的默认值 TypedMultipleChoiceField(MultipleChoiceField) coerce = lambda val: val #对选中的每一个值进行一次转换 empty_value = '' #空值的默认值 ComboField(Field) fields = () #使用多个验证,如下:即验证最大长度20,又验证邮箱格式 fields.ComboField(fields=[fields.CharField(max_length=20), fields.EmailField(), ]) SplitDateTimeField(MultiValueField) input_date_formats = None, #格式列表:['%Y--%m--%d', '%m%d/%Y', '%m/%d/%y'] input_time_formats = None #格式列表:['%H:%M:%S', '%H:%M:%S.%f', '%H:%M'] FilePathField(ChoiceField) #文件选项,目录下文件显示在页面中 path, #文件夹路径 match = None, #正则匹配 recursive = False, #递归下面的文件夹 allow_files = True, #允许文件 allow_folders = False, #允许文件夹 required = True, widget = None, label = None, initial = None, help_text = '' GenericIPAddressField protocol = 'both', both, ipv4, ipv6支持的IP格式 unpack_ipv4 = False 解析ipv4地址,如果是::ffff: 192.0.2.1时候,可解析为192.0.2.1, PS:protocol必须为both才能启用
3、常用选择插件
# 单radio,值为字符串 user = fields.CharField( initial=2, widget=widgets.Radioselect(choices=((1,'上海'),(2,'北京'),)) ) # 单radio,值为字符串 user = fields.ChoiceField( choices=((1, '上海'), (2, '北京'),), initial=2, widget=widgets.RadioSelect ) # 单select,值为字符串 user = fields.CharField( initial=2, widget=widgets.Select(choices=((1,'上海'),(2,'北京'),)) ) # 单select,值为字符串 user = fields.ChoiceField( choices=((1, '上海'), (2, '北京'),), initial=2, widget=widgets.Select ) # 多选select,值为列表 user = fields.MultipleChoiceField( choices=((1,'上海'),(2,'北京'),), initial=[1,], widget=widgets.SelectMultiple ) # 单checkbox user = fields.CharField( widget=widgets.CheckboxInput() ) # 多选checkbox,值为列表 user = fields.MultipleChoiceField( initial=[2, ], choices=((1, '上海'), (2, '北京'),), widget=widgets.CheckboxSelectMultiple )
二、表单渲染注册页面
views.py文件内容:
from django.shortcuts import render,HttpResponse from django import forms from django.forms import widgets class UserForm(forms.Form): user=forms.CharField(label="用户名", min_length=5, error_messages={"required":"不能为空","min_length":"最小长度不能小于5"}, widget=widgets.TextInput(attrs={"class":"form-control"}) ) tel=forms.CharField(label="手机号",max_length=8, widget=widgets.TextInput(attrs={"class":"form-control"})) email=forms.EmailField(label="邮箱", widget=widgets.TextInput(attrs={"class":"form-control"})) def reg(request): if request.method=="POST": #form=UserForm({"user":"alex999","tel":'123',"email":"111","123":123}) form=UserForm(request.POST) if form.is_valid(): print("====>",form.cleaned_data) # 校验成功的字段{"user":"alex999","tel":'123'} print("====>",form.errors) # 校验失败的字段 return HttpResponse("添加成功") else: # print("---->", form.cleaned_data) # # print("---->", type(form.errors)) # <class 'django.forms.utils.ErrorDict'> # print("---->", type(form.errors["user"])) # # print("---->", form.errors["user"][0]) # return render(request, 'reg.html',{"form":form}) form=UserForm() return render(request,'reg.html',{"form":form})
reg.html文件内容:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> <style> span{ color: red!important; } </style> <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> {#####################################方案一:#######################################} <div> <div> <div class="col-md-6 col-md-offset-3"> <form action="" method="post" novalidate> {% csrf_token %} {% for filed in form %} <div> <label for="">{{ filed.label }}</label> {{ filed }} </div> {% endfor %} <input type="submit"> </form> </div> </div> </div> {#####################################方案二:#######################################} <div> <div> <div class="col-md-6 col-md-offset-3"> <form action="" method="post" novalidate> {% csrf_token %} <div> 用户名: {{ form.user }} </div> <div> 邮箱: {{ form.email }} </div> <div> 手机号: {{ form.tel }} </div> <input type="submit"> </form> </div> </div> </div> {#####################################方案三:#######################################} <div> <div> <div class="col-md-6 col-md-offset-3"> <form action="" method="post" novalidate> {% csrf_token %} {{ form.as_p }} <input type="submit"> </form> </div> </div> </div> </body> </html>
三、示例
views.py文件内容:
from django.shortcuts import render,redirect from app01 import models from django.forms import Form from django.forms import fields from django.forms import widgets class TeacherForm(Form): #必须继承Form username = fields.CharField( required=True, #必填字段 error_messages={"required":"用户名不能为空!!"}, #显示中文错误提示 widget=widgets.TextInput(attrs={"placeholder":"用户名","class":"form-control"}) #自动生成input框 ) password = fields.CharField(required=True, error_messages={'required': '密码不能为空'}, widget=widgets.TextInput(attrs={'placeholder': '密码', 'class': 'form-control'})) # 不能为空 email = fields.EmailField( required=True, error_messages={"required":"邮箱不能为空!!","invalid":"无效的邮箱"}, widget=widgets.EmailInput(attrs={"placeholder": "邮箱", "class": "form-control"}) # 自动生成input框 ) #不能为空且邮箱格式要一致 def teacherindex(request): teacher_obj = models.UserInfo.objects.all() return render(request,"teacherindex.html",{"teacher_obj":teacher_obj}) def add(request): if request.method=="GET": form = TeacherForm() #只是让显示一个input框 return render(request,"add.html",{"form":form }) else: form = TeacherForm(data=request.POST) # print(form) #<QuerySet [<UserInfo: UserInfo object>, <UserInfo: UserInfo object>, <UserInfo: UserInfo object>]> if form.is_valid(): # 开始验证 # print('执行成功',form.cleaned_data) # 所有匹配成功,字典 form.cleaned_data['ut_id'] = 1 #要分的清是班主任还是讲师 models.UserInfo.objects.all().create(**form.cleaned_data) return redirect("/teacherindex/") else: # print("=====?",form.errors,type(form.errors))#返回失败的结果 # print(form.errors["username"][0]) #拿到返回失败的结果,渲染到页面 return render(request,"add.html",{"form":form})
前端html页面:
{% block right %} <h1>添加老师信息</h1> <hr> <form method="post" novalidate> {% csrf_token %} <p>姓名:{{ form.username }}</p>{{ form.errors.username.0 }} <p>密码:{{ form.password }}</p>{{ form.errors.password.0 }} <p>邮箱:{{ form.email }}</p>{{ form.errors.email.0 }} <p><input type="submit" value="提交"></p> </form> {% endblock %}
以上是关于Django之Form组件的主要内容,如果未能解决你的问题,请参考以下文章