Django的Form主要具有一下几大功能:

  • 生成HTML标签
  • 验证用户数据(显示错误信息)
  • HTML Form提交保留上次提交数据
  • 初始化页面显示内容

Form类的使用:

1、定义规则:

from django.forms import Form
from django.forms import fields
class xxx(Form):
xx = fields.CharField(max_lenghth=,min_lenghth=,required=True,error_message=)

2、使用:

obj = xxx(request.POST)
# 是否校验成功
v = obj.is_valid()
# html标签name属性 = Form类字段名 obj.is_valid()验证通过返回True,失败则返回False # 所有错误信息
obj.errors # 正确信息
obj.cleaned_data

登录和注册案例:

from django.shortcuts import render,HttpResponse,redirect

from django.forms import Form
from django.forms import fields
class LoginForm(Form):
# 正则验证: 不能为空,-
username = fields.CharField(
max_length=,
min_length=,
required=True,
error_messages={
'required': '用户名不能为空',
'min_length': '太短了',
'max_length': '太长了',
}
)
# 正则验证: 不能为空,+
password = fields.CharField(min_length=,required=True)
# email = fields.EmailField()
# email = fields.GenericIPAddressField()
# email = fields.IntegerField() def login(request):
if request.method == "GET":
return render(request,'login.html')
else:
obj = LoginForm(request.POST)
if obj.is_valid():
# 用户输入格式正确
print(obj.cleaned_data) # 字典类型
return redirect('http://www.baidu.com')
else:
# 用户输入格式错误
return render(request,'login.html',{'obj':obj})

views.py

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <form method="POST" action="/login/">
{% csrf_token %}
用户名:<input type="text" name="username">{{ obj.errors.username. }}<br>
密码 &nbsp;&nbsp;:<input type="password" name="password">{{ obj.errors.password. }}<br>
<input type="submit" value="提交"> </form> </body>
</html>

login.html

基于Form和Ajax提交实现用户登录案例:两种验证方式

from django.shortcuts import render,redirect,HttpResponse
from django.forms import Form
from django.forms import fields
from django.forms import widgets class LoginForm(Form):
user = fields.CharField(required=True)
pwd = fields.CharField(min_length=) def login(request):
if request.method == 'GET':
return render(request,'login.html')
else:
obj = LoginForm(request.POST)
if obj.is_valid():
print(obj.cleaned_data)
return redirect('http://www.baidu.com')
return render(request,'login.html',{'obj': obj}) def ajax_login(request):
import json
ret = {'status': True,'msg': None}
obj = LoginForm(request.POST)
if obj.is_valid():
print(obj.cleaned_data)
else:
# print(obj.errors) # obj.errors对象
ret['status'] = False
ret['msg'] = obj.errors
v = json.dumps(ret)
return HttpResponse(v) #
# class TestForm(Form):
# t1 = fields.CharField(
# required=True,
# max_length=,
# min_length=,
# error_messages={
# 'required': '不能为空',
# 'max_length': '太长',
# 'min_length': '太短',
# }
# )
# t2 = fields.IntegerField(
# min_value=,
# max_value=,
# error_messages={
# 'required': 't2不能为空',
# 'invalid': 't2格式错误,必须是数字',
# 'min_value': '必须大于10',
# 'max_value': '必须小于1000',
# },
# )
# t3 = fields.EmailField(
# error_messages={
# 'required': 't3不能为空',
# 'invalid': 't3格式错误,必须是邮箱格式',
# }
# ) class TestForm(Form):
t1 = fields.CharField(required=True,max_length=,min_length=,
error_messages={
'required': '不能为空',
'max_length': '太长',
'min_length': '太短',
}
)
t2 = fields.EmailField() def test(request):
if request.method == "GET":
obj = TestForm()
return render(request,'test.html',{'obj': obj})
else:
obj = TestForm(request.POST)
if obj.is_valid():
print(obj.cleaned_data)
else:
print(obj.errors)
return render(request,'test.html',{'obj':obj}) class RegiterForm(Form):
user = fields.CharField(min_length=)
email = fields.EmailField()
password = fields.CharField()
phone = fields.RegexField('139\d+') def register(request):
if request.method == 'GET':
obj = RegiterForm()
return render(request,'register.html',{'obj':obj})
else:
obj = RegiterForm(request.POST)
if obj.is_valid():
print(obj.cleaned_data)
else:
print(obj.errors)
return render(request,'register.html',{'obj':obj})

views.py

"""s4day77 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
. Add an import: from my_app import views
. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
. Add an import: from other_app.views import Home
. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
. Import the include() function: from django.conf.urls import url, include
. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/', views.login),
url(r'^ajax_login/', views.ajax_login),
url(r'^test/', views.test),
url(r'^register/', views.register),
]

urls.py

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>用户登录</h1>
<form id="f1" action="/login/" method="POST">
{% csrf_token %}
<p>
<input type="text" name="user" />{{ obj.errors.user. }}
</p>
<p>
<input type="password" name="pwd" />{{ obj.errors.pwd. }}
</p>
<input type="submit" value="提交" />
<a onclick="submitForm();">提交</a>
</form>
<script src="/static/jquery-1.12.4.js"></script>
<script>
function submitForm(){
$('.c1').remove();
$.ajax({
url: '/ajax_login/',
type: 'POST',
data: $('#f1').serialize(),// user=alex&pwd=456&csrftoen=dfdf\
dataType:"JSON",
success:function(arg){
console.log(arg);
if(arg.status){ }else{
$.each(arg.msg,function(index,value){
console.log(index,value);
var tag = document.createElement('span');
tag.innerHTML = value[];
tag.className = 'c1';
$('#f1').find('input[name="'+ index +'"]').after(tag);
})
}
}
})
}
</script>
</body>
</html>

login.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="/test/" method="POST" novalidate>
{% csrf_token %}
<p>
{{ obj.t1 }}{{ obj.errors.t1. }}
</p>
<p>
{{ obj.t2 }}{{ obj.errors.t2. }}
</p>
<input type="submit" value="提交" />
</form>
</body>
</html>

test.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body> <form action="/register/" method="POST" novalidate>
{% csrf_token %}
<p>
{{ obj.user }} {{ obj.errors.user. }}
</p>
<p>
{{ obj.email }} {{ obj.errors.email. }}
</p>
<p>
{{ obj.password }} {{ obj.errors.password. }}
</p>
<p>
{{ obj.phone }} {{ obj.errors.phone. }}
</p>
<input type="submit" value="提交" />
</form>
</body>
</html>

register.html

总结:

- Ajax,仅用验证功能
- Form,验证功能,生成HTML标签

班级、学生案例:

from django.db import models

class Classes(models.Model):
title = models.CharField(max_length=) class Student(models.Model):
name = models.CharField(max_length=)
email = models.CharField(max_length=)
age = models.IntegerField(max_length=)
cls = models.ForeignKey('Classes') class Teacher(models.Model):
tname = models.CharField(max_length=)
c2t = models.ManyToManyField('Classes')

models.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 ClassForm(Form):
title = fields.RegexField('全栈\d+') def class_list(request):
cls_list = models.Classes.objects.all()
return render(request,'class_list.html',{'cls_list':cls_list}) def add_class(request):
if request.method == "GET":
obj = ClassForm()
return render(request,'add_class.html',{'obj': obj})
else:
obj = ClassForm(request.POST)
if obj.is_valid():
# obj.cleaned_data # 字典
# 数据库创建一条数据
# print(obj.cleaned_data)
# models.Classes.objects.create(title=obj.cleaned_data['tt']) models.Classes.objects.create(**obj.cleaned_data)
return redirect('/class_list/')
return render(request,'add_class.html',{'obj': obj}) def edit_class(request,nid):
if request.method == "GET":
row = models.Classes.objects.filter(id=nid).first()
# 让页面显示初始值
# obj = ClassForm(data={'title': 'asdfasdfasdfas'})
obj = ClassForm(initial={'title': row.title})
return render(request,'edit_class.html',{'nid': nid,'obj':obj})
else:
obj = ClassForm(request.POST)
if obj.is_valid():
models.Classes.objects.filter(id=nid).update(**obj.cleaned_data)
return redirect('/class_list/')
return render(request,'edit_class.html',{'nid': nid,'obj':obj}) class StudentForm(Form):
name = fields.CharField(
min_length=,
max_length=,
widget=widgets.TextInput(attrs={'class': 'form-control'})
)
email = fields.EmailField(widget=widgets.TextInput(attrs={'class': 'form-control'}))
age = fields.IntegerField(min_value=,max_value=,widget=widgets.TextInput(attrs={'class': 'form-control'}))
cls_id = fields.IntegerField(
# widget=widgets.Select(choices=[(,'上海'),(,'北京')])
widget=widgets.Select(choices=models.Classes.objects.values_list('id','title'),attrs={'class': 'form-control'})
) def student_list(request): stu_list = models.Student.objects.all()
return render(request,'student_list.html',{'stu_list':stu_list}) def add_student(request):
if request.method == "GET":
obj = StudentForm()
return render(request,'add_student.html',{'obj':obj})
else:
obj = StudentForm(request.POST)
if obj.is_valid():
models.Student.objects.create(**obj.cleaned_data)
return redirect('/student_list/')
return render(request,'add_student.html',{'obj':obj}) def edit_student(request,nid):
if request.method == "GET":
row = models.Student.objects.filter(id=nid).values('name','email','age','cls_id').first()
obj = StudentForm(initial=row)
return render(request,'edit_student.html',{'nid':nid,'obj': obj})
else:
obj = StudentForm(request.POST)
if obj.is_valid():
models.Student.objects.filter(id=nid).update(**obj.cleaned_data)
return redirect('/student_list/')
return render(request,'edit_student.html',{'nid':nid,'obj': obj})

views.py

"""s4day77example URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
. Add an import: from my_app import views
. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
. Add an import: from other_app.views import Home
. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
. Import the include() function: from django.conf.urls import url, include
. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^class_list/', views.class_list),
url(r'^add_class/', views.add_class),
url(r'^edit_class/(\d+)/', views.edit_class), url(r'^student_list/', views.student_list),
url(r'^add_student/', views.add_student),
url(r'^edit_student/(\d+)/', views.edit_student),
]

urls.py

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>班级列表</h1>
<div>
<a href="/add_class/">添加</a>
</div>
<ul>
{% for row in cls_list %}
<li>{{ row.title }} <a href="/edit_class/{{ row.id }}/">编辑</a> </li>
{% endfor %}
</ul>
</body>
</html>

class_list.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>添加班级</h1>
<form method="POST" action="/add_class/" novalidate>
{% csrf_token %}
{{ obj.title }} {{ obj.errors.title. }}
<input type="submit" value="提交" />
</form>
</body>
</html>

add_class.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>编辑班级</h1>
<form method="POST" action="/edit_class/{{ nid }}/">
{% csrf_token %}
<p>
{{ obj.title }} {{ obj.errors.title. }}
</p>
<input type='submit' value="提交" />
</form>
</body>
</html>

edit_class.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>学生列表</h1>
<a href="/add_student/">添加</a>
<ul>
{% for row in stu_list %}
<li>{{ row.name }}-{{ row.email }}-{{ row.age }}-{{ row.cls_id }}-{{ row.cls.title }} <a href="/edit_student/{{ row.id }}/">编辑</a></li>
{% endfor %}
</ul>
</body>
</html>

student_list.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>添加学生</h1>
<form action="/add_student/" method="POST">
{% csrf_token %}
<p>
{{ obj.name }}
</p>
<p>
{{ obj.email }}
</p>
<p>
{{ obj.age }}
</p>
<p>
{{ obj.cls_id }}
</p>
<input type="submit" value="提交" />
</form>
</body>
</html>

add_student.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="/static/bootstrap-3.3.5-dist/css/bootstrap.css"/>
</head>
<body> <div style="width: 500px;margin: 0 auto;">
<form class="form-horizontal" method="POST" action="/edit_student/{{ nid }}/">
{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 control-label">姓名:</label> <div class="col-sm-10">
{{ obj.name }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">邮箱:</label> <div class="col-sm-10">
{{ obj.email }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">年龄:</label> <div class="col-sm-10">
{{ obj.age }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">班级:</label> <div class="col-sm-10">
{{ obj.cls_id }}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="提交" />
</div>
</div>
</form>
</div>
</body>
</html>

edit_student.html

老师案例:

class TeacherForm(Form):
name = fields.CharField()
xx = fields.MultipleChoiceField(
widget=widgets.SelectMultiple
)
def __init__(self,*args,**kwargs):
super(TeacherForm,self).__init__(*args,**kwargs)
self.fields['xx'].choices = models.classes.objects.values_list('id','title') def teacher_list(request):
tea_list = models.teacher.objects.all()
return render(request,'teacher_list.html',{'tea_list':tea_list}) def add_teacher(request):
if request.method == "GET":
obj = TeacherForm()
return render(request,'add_teacher.html',{'obj':obj})
else:
obj = TeacherForm(request.POST)
# print(obj)
if obj.is_valid():
xx = obj.cleaned_data.pop('xx')
row = models.teacher.objects.create(**obj.cleaned_data)
row.c2t.add(*xx)
return redirect('/teacher_list/')
else:
print(obj.errors)
return render(request,'add_teacher.html',{'obj':obj}) def edit_teacher(request,nid):
if request.method == "GET":
row = models.teacher.objects.filter(id=nid).first()
class_ids = row.c2t.values_list('id')
id_list = list(zip(*class_ids))[] if list(zip(*class_ids)) else []
obj =TeacherForm(initial={'name':row.name,'xx':id_list})
return render(request,'edit_teacher.html',{'obj':obj,'nid':nid})
else:
obj = TeacherForm(request.POST)
if obj.is_valid():
cls_list = obj.cleaned_data.pop('xx')
row = models.teacher.objects.filter(id=nid)
row.update(**obj.cleaned_data)
row.first().c2t.set(cls_list)
return redirect('/teacher_list/')
else:
print(obj.errors)
return render(request,'edit_teacher.html',{'obj':obj,'nid':nid})

views.py

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <h1>老师列表</h1>
<a href="/add_teacher/">添加</a>
<table>
<tr>
<th>ID</th>
<th>老师姓名</th>
<th>班级</th>
</tr>
{% for row in tea_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.name }}</td>
<td>
{% for row in row.c2t.all %}
{{ row.title }}
{% endfor %}
</td>
<td>
<a href="/edit_teacher/{{ row.id }}">编辑</a>
</td>
</tr>
{% endfor %}
</table> </body>
</html>

teacher_list.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <form method="POST" action="/add_teacher/">
{% csrf_token %}
{{ obj.name }}{{ obj.errors.name. }}
{{ obj.xx }}{{ obj.errors.xx. }}
<input type="submit" value="提交">
</form> </body>
</html>

add_teacher.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <h1>编辑老师</h1>
<form method="POST" action="/edit_teacher/{{ nid }}/">
{% csrf_token %}
{{ obj.name }}{{ obj.errors.name. }}
{{ obj.xx }} {{ obj.errors.xx. }}
<input type="submit" value="提交">
</form> </body>
</html>

edit_teacher.html

"""class_student URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
. Add an import: from my_app import views
. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
. Add an import: from other_app.views import Home
. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
. Import the include() function: from django.conf.urls import url, include
. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^class_list/', views.class_list),
url(r'^add_class/', views.add_class),
url(r'^edit_class/(\d+)/', views.edit_class), url(r'^student_list/', views.student_list),
url(r'^add_student/', views.add_student),
url(r'^edit_student/(\d+)/', views.edit_student), url(r'^teacher_list/', views.teacher_list),
url(r'^add_teacher/', views.add_teacher),
url(r'^edit_teacher/(\d+)/', views.edit_teacher), url(r'^f1/',views.f1),
url(r'^f2/',views.f2),
]

urls.py

Form组件的上传功能

from django.shortcuts import render,redirect,HttpResponse
from django.forms import Form
from django.forms import fields
from django.forms import widgets
from app01 import models
import os class F2Form(Form):
user = fields.CharField()
fafafa = fields.FileField() def f1(request):
if request.method=="GET":
return render(request,'f1.html')
else:
print(request.FILES)
file_obj = request.FILES.get("fafafa")
f = open(os.path.join('static',file_obj.name),'wb')
for chunk in file_obj.chunks():
f.write(chunk)
f.close()
return render(request,'f1.html') def f2(request):
if request.method == "GET":
obj = F2Form()
return render(request,'f2.html',{'obj':obj})
else:
obj = F2Form(data=request.POST,files=request.FILES)
if obj.is_valid():
print(obj.cleaned_data.get("fafafa").name)
print(obj.cleaned_data.get("fafafa").size)
return render(request,'f2.html',{'obj':obj}) views.py

views.py

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <form method="POST" action="/f1/" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="fafafa">
<input type="submit" value="提交">
</form> </body>
</html>

f1.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <form method="POST" action="/f2/" enctype="multipart/form-data">
{% csrf_token %}
{{ obj.user }}
{{ obj.fafafa }}
<input type="submit" value="提交">
</form> </body>
</html>

f2.html

Form类的字段和插件

创建Form类时,主要涉及到 【字段】 和 【插件】,字段用于对用户请求数据的验证,插件用于自动生成HTML;

1、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, 最小值 FloatField(IntegerField)
... 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': '...'} EmailField(CharField)
... FileField(Field)
allow_empty_file=False 是否允许空文件 ImageField(FileField)
...
注:需要PIL模块,pip3 install Pillow
以上两个字典使用时,需要注意两点:
- form表单中 enctype="multipart/form-data"
- view函数中 obj = MyForm(request.POST, request.FILES) URLField(Field)
... BooleanField(Field)
... NullBooleanField(BooleanField)
... 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= '' 空值的默认值 MultipleChoiceField(ChoiceField)
... TypedMultipleChoiceField(MultipleChoiceField)
coerce = lambda val: val 对选中的每一个值进行一次转换
empty_value= '' 空值的默认值 ComboField(Field)
fields=() 使用多个验证,如下:即验证最大长度20,又验证邮箱格式
fields.ComboField(fields=[fields.CharField(max_length=20), fields.EmailField(),]) MultiValueField(Field)
PS: 抽象类,子类中可以实现聚合多个字典去匹配一个值,要配合MultiWidget使用 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才能启用 SlugField(CharField) 数字,字母,下划线,减号(连字符)
... UUIDField(CharField) uuid类型
...

注:UUID是根据MAC以及当前时间等创建的不重复的随机字符串

>>> import uuid

    # make a UUID based on the host ID and current time
>>> uuid.uuid1() # doctest: +SKIP
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') # make a random UUID
>>> uuid.uuid4() # doctest: +SKIP
UUID('16fd2706-8baf-433b-82eb-8c7fada847da') # make a UUID using a SHA- hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') # convert a UUID to a string of hex digits in standard form
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f' # get the raw bytes of the UUID
>>> x.bytes
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' # make a UUID from a -byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

2、Django内置插件:

TextInput(Input)
NumberInput(TextInput)
EmailInput(TextInput)
URLInput(TextInput)
PasswordInput(TextInput)
HiddenInput(TextInput)
Textarea(Widget)
DateInput(DateTimeBaseInput)
DateTimeInput(DateTimeBaseInput)
TimeInput(DateTimeBaseInput)
CheckboxInput
Select
NullBooleanSelect
SelectMultiple
RadioSelect
CheckboxSelectMultiple
FileInput
ClearableFileInput
MultipleHiddenInput
SplitDateTimeWidget
SplitHiddenDateTimeWidget
SelectDateWidget

常用选择插件

# 单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
# )

在使用选择标签时,需要注意choices的选项可以从数据库中获取,但是由于是静态字段 ***获取的值无法实时更新***,那么需要自定义构造方法从而达到此目的。

方式一:

from django.forms import Form
from django.forms import widgets
from django.forms import fields
from django.core.validators import RegexValidator class MyForm(Form): user = fields.ChoiceField(
# choices=((1, '上海'), (2, '北京'),),
initial=2,
widget=widgets.Select
) def __init__(self, *args, **kwargs):
super(MyForm,self).__init__(*args, **kwargs)
# self.fields['user'].widget.choices = ((1, '上海'), (2, '北京'),)
# 或
self.fields['user'].widget.choices = models.Classes.objects.all().value_list('id','caption')

方式二:

使用django提供的ModelChoiceField和ModelMultipleChoiceField字段来实现

from django import forms
from django.forms import fields
from django.forms import widgets
from django.forms import models as form_model
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator class FInfo(forms.Form):
authors = form_model.ModelMultipleChoiceField(queryset=models.NNewType.objects.all())
# authors = form_model.ModelChoiceField(queryset=models.NNewType.objects.all())zidi

自定义验证规则

方式一:

from django.forms import Form
from django.forms import widgets
from django.forms import fields
from django.core.validators import RegexValidator class MyForm(Form):
user = fields.CharField(
validators=[RegexValidator(r'^[0-9]+$', '请输入数字'), RegexValidator(r'^159[0-9]+$', '数字必须以159开头')],
)

方式二:

import re
from django.forms import Form
from django.forms import widgets
from django.forms import fields
from django.core.exceptions import ValidationError # 自定义验证规则
def mobile_validate(value):
mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
if not mobile_re.match(value):
raise ValidationError('手机号码格式错误') class PublishForm(Form): title = fields.CharField(max_length=20,
min_length=5,
error_messages={'required': '标题不能为空',
'min_length': '标题最少为5个字符',
'max_length': '标题最多为20个字符'},
widget=widgets.TextInput(attrs={'class': "form-control",
'placeholder': '标题5-20个字符'})) # 使用自定义验证规则
phone = fields.CharField(validators=[mobile_validate, ],
error_messages={'required': '手机不能为空'},
widget=widgets.TextInput(attrs={'class': "form-control",
'placeholder': u'手机号码'})) email = fields.EmailField(required=False,
error_messages={'required': u'邮箱不能为空','invalid': u'邮箱格式错误'},
widget=widgets.TextInput(attrs={'class': "form-control", 'placeholder': u'邮箱'}))

方法三:自定义方法

from django import forms
from django.forms import fields
from django.forms import widgets
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator class FInfo(forms.Form):
username = fields.CharField(max_length=5,
validators=[RegexValidator(r'^[0-9]+$', 'Enter a valid extension.', 'invalid')], )
email = fields.EmailField() def clean_username(self):
"""
Form中字段中定义的格式匹配完之后,执行此方法进行验证
:return:
"""
value = self.cleaned_data['username']
if "666" in value:
raise ValidationError('666已经被玩烂了...', 'invalid')
return value

方式四:同时生成多个标签进行验证

from django.forms import Form
from django.forms import widgets
from django.forms import fields from django.core.validators import RegexValidator ############## 自定义字段 ##############
class PhoneField(fields.MultiValueField):
def __init__(self, *args, **kwargs):
# Define one message for all fields.
error_messages = {
'incomplete': 'Enter a country calling code and a phone number.',
}
# Or define a different message for each field.
f = (
fields.CharField(
error_messages={'incomplete': 'Enter a country calling code.'},
validators=[
RegexValidator(r'^[0-9]+$', 'Enter a valid country calling code.'),
],
),
fields.CharField(
error_messages={'incomplete': 'Enter a phone number.'},
validators=[RegexValidator(r'^[0-9]+$', 'Enter a valid phone number.')],
),
fields.CharField(
validators=[RegexValidator(r'^[0-9]+$', 'Enter a valid extension.')],
required=False,
),
)
super(PhoneField, self).__init__(error_messages=error_messages, fields=f, require_all_fields=False, *args,
**kwargs) def compress(self, data_list):
"""
当用户验证都通过后,该值返回给用户
:param data_list:
:return:
"""
return data_list ############## 自定义插件 ##############
class SplitPhoneWidget(widgets.MultiWidget):
def __init__(self):
ws = (
widgets.TextInput(),
widgets.TextInput(),
widgets.TextInput(),
)
super(SplitPhoneWidget, self).__init__(ws) def decompress(self, value):
"""
处理初始值,当初始值initial不是列表时,调用该方法
:param value:
:return:
"""
if value:
return value.split(',')
return [None, None, None]

初始化数据

在Web应用程序中开发编写功能时,时常用到获取数据库中的数据并将值初始化在HTML中的标签上。

1、Form

from django.forms import Form
from django.forms import widgets
from django.forms import fields
from django.core.validators import RegexValidator class MyForm(Form):
user = fields.CharField() city = fields.ChoiceField(
choices=((1, '上海'), (2, '北京'),),
widget=widgets.Select
)

2、Views

from django.shortcuts import render, redirect
from .forms import MyForm def index(request):
if request.method == "GET":
values = {'user': 'root', 'city': 2}
obj = MyForm(values) return render(request, 'index.html', {'form': obj})
elif request.method == "POST":
return redirect('http://www.google.com')
else:
return redirect('http://www.google.com')

3、HTML

<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.user }} {{ form.user.errors }}</p>
<p>{{ form.city }} {{ form.city.errors }}</p> <input type="submit"/>
</form>

  

Django 的 Form组件的更多相关文章

  1. Django之Form组件

    Django之Form组件 本节内容 基本使用 form中字段和插件 自定义验证规则 动态加载数据到form中 1. 基本使用 django中的Form组件有以下几个功能: 生成HTML标签 验证用户 ...

  2. Python之路【第二十一篇】:Django之Form组件

    Django之Form组件   Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 小试牛刀 1. ...

  3. 第十一篇:web之Django之Form组件

    Django之Form组件   Django之Form组件 本节内容 基本使用 form中字段和插件 自定义验证规则 动态加载数据到form中 1. 基本使用 django中的Form组件有以下几个功 ...

  4. python Django之Form组件

    python Django之Form组件 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 小试 ...

  5. Django之Form组件(一)

    Django之Form组件(一) Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 基本操作:字 ...

  6. python框架之Django(10)-Form组件

    介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来.与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否输入,输入 ...

  7. 〖Python〗-- Django的Form组件

    [Django的Form组件] Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 Form类的使 ...

  8. Django之Form组件验证

    今天来谈谈Django的Form组件操作 Django中的Form一般有两种功能: ·输入html ·验证用户输入 Form验证流程 ·定义规则(是一个类)    ·前端把数据提交过来 ·匹配规则 · ...

  9. django 使用form组件提交数据之form表单提交

    django的form组件可以减少后台在进行一些重复性的验证工作,极大降低开发效率. 最近遇到一个问题: 当使用form表单提交数据后,如果数据格式不符合后台定义的规则,需要重新在前端页面填写数据. ...

  10. web之Django之Form组件

    Django之Form组件 本节内容 基本使用 form中字段和插件 自定义验证规则 动态加载数据到form中 1. 基本使用 django中的Form组件有以下几个功能: 生成HTML标签 验证用户 ...

随机推荐

  1. 转载:IE下div使用margin:0px auto不居中的原因

    转自:http://www.blogjava.net/sealyu/archive/2010/01/08/308640.html 一般在将div居中显示时,使用css: divX {margin:0  ...

  2. get 方法向后台提交中文乱码问题

    前端js代码 function searchAll(){      var contentStr = $('#contentStr_id').val();      contentStr =encod ...

  3. 记录下自己常用的全框架HTML代码

    纯粹记录下,没有任何意义. 也不推荐使用 <frameset rows="> <frame src=" name="topFrame" scr ...

  4. SVN目录权限设置

    ---恢复内容开始--- 如图,这里我建的项目库为myRepositories,其下边又有许多文件,现在要分别对每个文件进行svn权限配置. 配置 进入上面生成的文件夹conf下,进行配置.有以下几个 ...

  5. windows下dubbo-admin和zookeeper安装部署

    1.   概述 ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂 ...

  6. findmaven的英文版本号上线了

    http://www.findmaven.net是一个查找Jar和查找Maven的Maven仓库搜索引擎.它能够依据Java开发人员提供的Class名或者Jar名找到包括它的Jar,同一时候提供Jar ...

  7. MyException--org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ###

    org.apache.ibatis.exceptions.PersistenceException:  ### Error building SqlSession. ### The error may ...

  8. 查看内存使用情况:free

    free命令用于显示内存的使用情况,常见用法如下: [root@localhost ~]$ free # 以KB为单位显示内存使用情况 [root@localhost ~]$ free -m # 以M ...

  9. 什么叫做hack

    由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到 ...

  10. js里面函数的内部属性

    1.arguments用來存放传输参数的集合,可以被调用多次,每次数組都不一样,增强了函数的强壮性 实例: function calc() { var sum = 0; /*参数为一个时候*/ if ...