django form 和modelform
本节目录
我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来。
与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否输入,输入的长度和格式等正不正确。如果用户输入的内容有错误就需要在页面上相应的位置显示对应的错误信息.。
Django form组件就实现了上面所述的功能。
总结一下,其实form组件的主要功能如下:
生成页面可用的HTML标签
对用户提交的数据进行校验
保留上次输入内容
普通方式手写注册功能
views.py
# 注册 def register(request): error_msg = "" if request.method == "POST": username = request.POST.get("name") pwd = request.POST.get("pwd") # 对注册信息做校验 if len(username) < 6: # 用户长度小于6位 error_msg = "用户名长度不能小于6位" else: # 将用户名和密码存到数据库 return HttpResponse("注册成功") return render(request, "register.html", {"error_msg": error_msg})
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册页面</title> </head> <body> <form action="/reg/" method="post"> {% csrf_token %} <p> 用户名: <input type="text" name="name"> </p> <p> 密码: <input type="password" name="pwd"> </p> <p> <input type="submit" value="注册"> <p style="color: red">{{ error_msg }}</p> </p> </form> </body> </html>
使用form组件实现注册功能
views.py
先定义好一个RegForm类:
from django import forms# 按照Django form组件的要求自己写一个类
class RegForm(forms.Form):
name = forms.CharField(label="用户名") #form字段的名称写的是什么,那么前端生成input标签的时候,input标签的name属性的值就是什么
pwd = forms.CharField(label="密码")
再写一个视图函数:
# 使用form组件实现注册方式 def register2(request): form_obj = RegForm() if request.method == "POST": # 实例化form对象的时候,把post提交过来的数据直接传进去 form_obj = RegForm(data=request.POST) #既然传过来的input标签的name属性值和form类对应的字段名是一样的,所以接过来后,form就取出对应的form字段名相同的数据进行form校验 # 调用form_obj校验数据的方法 if form_obj.is_valid(): return HttpResponse("注册成功") return render(request, "register2.html", {"form_obj": form_obj})
login2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册2</title> </head> <body> <form action="/reg2/" method="post" novalidate autocomplete="off"> #novalidate 告诉前端form表单,不要对输入的内容做校验 {% csrf_token %}
#{{ form_obj.as_p }} 直接写个这个,下面的用户名和密码的标签不自己写,你看看效果 <div> <label for="{{ form_obj.name.id_for_label }}">{{ form_obj.name.label }}</label> {{ form_obj.name }} {{ form_obj.name.errors.0 }} #errors是这个字段所有的错误,我就用其中一个错误提示就可以了,再错了再提示,并且不是给你生成ul标签了,单纯的是错误文本
{{ form_obj.errors }} #这是全局的所有错误,找对应字段的错误,就要form_obj.字段名 </div> <div> <label for="{{ form_obj.pwd.id_for_label }}">{{ form_obj.pwd.label }}</label> {{ form_obj.pwd }} {{ form_obj.pwd.errors.0 }} </div> <div> <input type="submit" class="btn btn-success" value="注册"> </div> </form> </body> </html>
看网页效果发现 也验证了form的功能:
前端页面是form类的对象生成的 -->生成HTML标签功能
当用户名和密码输入为空或输错之后 页面都会提示 -->用户提交校验功能
当用户输错之后 再次输入 上次的内容还保留在input框 -->保留上次输入内容
创建Form类时,主要涉及到 【字段】 和 【插件】,字段用于对用户请求数据的验证,插件用于自动生成HTML;
initial
初始值,input框里面的初始值。
class LoginForm(forms.Form): username = forms.CharField( min_length=8, label="用户名", initial="张三" # 设置默认值 ) pwd = forms.CharField(min_length=6, label="密码")
error_messages
重写错误信息。
class LoginForm(forms.Form): username = forms.CharField( min_length=8, label="用户名", initial="张三", error_messages={ "required": "不能为空", "invalid": "格式错误", "min_length": "用户名最短8位" } ) pwd = forms.CharField(min_length=6, label="密码")
password
class LoginForm(forms.Form): ... pwd = forms.CharField( min_length=6, label="密码", widget=forms.widgets.PasswordInput(attrs={'class': 'c1'}, render_value=True) #这个密码字段和其他字段不一样,默认在前端输入数据错误的时候,点击提交之后,默认是不保存的原来数据的,但是可以通过这个render_value=True让这个字段在前端保留用户输入的数据 )
radioSelect
单radio值为字符串
class LoginForm(forms.Form): username = forms.CharField( #其他选择框或者输入框,基本都是在这个CharField的基础上通过插件来搞的 min_length=8, label="用户名", initial="张三", error_messages={ "required": "不能为空", "invalid": "格式错误", "min_length": "用户名最短8位" } ) pwd = forms.CharField(min_length=6, label="密码") gender = forms.fields.ChoiceField( choices=((1, "男"), (2, "女"), (3, "保密")), label="性别", initial=3, widget=forms.widgets.RadioSelect() )
单选Select
class LoginForm(forms.Form): ... hobby = forms.fields.ChoiceField( #注意,单选框用的是ChoiceField,并且里面的插件是Select,不然验证的时候会报错, Select a valid choice的错误。 choices=((1, "篮球"), (2, "足球"), (3, "双色球"), ), label="爱好", initial=3, widget=forms.widgets.Select() )
多选Select
class LoginForm(forms.Form): ... hobby = forms.fields.MultipleChoiceField( #多选框的时候用MultipleChoiceField,并且里面的插件用的是SelectMultiple,不然验证的时候会报错。 choices=((1, "篮球"), (2, "足球"), (3, "双色球"), ), label="爱好", initial=[1, 3], widget=forms.widgets.SelectMultiple() )
单选checkbox
class LoginForm(forms.Form): ... keep = forms.fields.ChoiceField( label="是否记住密码", initial="checked", widget=forms.widgets.CheckboxInput() )
单选checkbox示例:
#单选的checkbox class TestForm2(forms.Form): keep = forms.ChoiceField( choices=( ('True',1), ('False',0), ),label</span>=<span style="color: #800000;">"</span><span style="color: #800000;">是否7天内自动登录</span><span style="color: #800000;">"</span><span style="color: #000000;">, initial</span>=<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;">, widget</span>=<span style="color: #000000;">forms.widgets.CheckboxInput(), ) 选中:</span><span style="color: #800000;">'</span><span style="color: #800000;">True</span><span style="color: #800000;">'</span> <span style="color: #008000;">#</span><span style="color: #008000;">form只是帮我们做校验,校验选择内容的时候,就是看在没在我们的choices里面,里面有这个值,表示合法,没有就不合法</span> 没选中:<span style="color: #800000;">'</span><span style="color: #800000;">False</span><span style="color: #800000;">'</span> ---保存到数据库里面 keep:<span style="color: #800000;">'</span><span style="color: #800000;">True</span><span style="color: #800000;">'</span> <span style="color: #0000ff;">if</span> keep == <span style="color: #800000;">'</span><span style="color: #800000;">True</span><span style="color: #800000;">'</span><span style="color: #000000;">: session 设置有效期7天 </span><span style="color: #0000ff;">else</span><span style="color: #000000;">: </span><span style="color: #0000ff;">pass</span></pre>
多选checkbox
class LoginForm(forms.Form): ... hobby = forms.fields.MultipleChoiceField( choices=((1, "篮球"), (2, "足球"), (3, "双色球"),), label="爱好", initial=[1, 3], widget=forms.widgets.CheckboxSelectMultiple() )
date类型
from django import forms from django.forms import widgets class BookForm(forms.Form): date = forms.DateField(widget=widgets.TextInput(attrs={'type':'date'})) #必须指定type,不然不能渲染成选择时间的input框
choice字段注意事项
在使用选择标签时,需要注意choices的选项可以配置从数据库中获取,但是由于是静态字段 获取的值无法实时更新,需要重写构造方法从而实现choice实时更新。
方式一:
from django.forms import Form from django.forms import widgets from django.forms import fieldsclass MyForm(Form):
user </span>=<span style="color: #000000;"> fields.ChoiceField( </span><span style="color: #008000;">#</span><span style="color: #008000;"> choices=((1, '上海'), (2, '北京'),),</span> initial=2<span style="color: #000000;">, widget</span>=<span style="color: #000000;">widgets.Select ) </span><span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span>(self, *args, **<span style="color: #000000;">kwargs): super(MyForm,self).</span><span style="color: #800080;">__init__</span>(*args, **<span style="color: #000000;">kwargs) #注意重写init方法的时候,*args和**kwargs一定要给人家写上,不然会出问题,并且验证总是不能通过,还不显示报错信息 </span><span style="color: #008000;">#</span><span style="color: #008000;"> self.fields['user'].choices = ((1, '上海'), (2, '北京'),)</span> <span style="color: #008000;">#</span><span style="color: #008000;"> 或</span> self.fields[<span style="color: #800000;">'</span><span style="color: #800000;">user</span><span style="color: #800000;">'</span>].choices = models.Classes.objects.all().values_list(<span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">caption</span><span style="color: #800000;">'</span>)</pre>
方式二:
from django import forms from django.forms import fields from django.forms import models as form_modelclass FInfo(forms.Form):
authors = forms.ModelMultipleChoiceField(queryset=models.NNewType.objects.all()) # 多选
#或者下面这种方式,通过forms里面的models中提供的方法也是一样的。
authors = form_model.ModelMultipleChoiceField(queryset=models.NNewType.objects.all()) # 多选 #authors = form_model.ModelChoiceField(queryset=models.NNewType.objects.all()) # 单选
#或者,forms.ModelChoiceField(queryset=models.Publisth.objects.all(),widget=forms.widgets.Select()) 单选
#
authors = forms.ModelMultipleChoiceField(
queryset=models.Author.objects.all(),
widget = forms.widgets.Select(attrs={'class': 'form-control'}
))
#如果用这种方式,别忘了model表中,NNEWType的__str__方法要写上,不然选择框里面是一个个的object对象
内置字段:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label标签或显示内容 initial=None, 初始值 help_text='', 帮助信息(在标签旁边显示) error_messages=None, 错误信息 {'required': '不能为空', 'invalid': '格式错误'} 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:12DurationField(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.ModelMultipleChoiceFieldTypedChoiceField(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类型
复制代码
RegexValidator验证器
from django.forms import Form from django.forms import widgets from django.forms import fields from django.core.validators import RegexValidatorclass MyForm(Form):
user = fields.CharField(
validators=[RegexValidator(r'[1]+$', '请输入数字'), 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 </span>= fields.CharField(max_length=20<span style="color: #000000;">, min_length</span>=5<span style="color: #000000;">, error_messages</span>={<span style="color: #800000;">'</span><span style="color: #800000;">required</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">标题不能为空</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">min_length</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">标题最少为5个字符</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">max_length</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">标题最多为20个字符</span><span style="color: #800000;">'</span><span style="color: #000000;">}, widget</span>=widgets.TextInput(attrs={<span style="color: #800000;">'</span><span style="color: #800000;">class</span><span style="color: #800000;">'</span>: <span style="color: #800000;">"</span><span style="color: #800000;">form-control</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">placeholder</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">标题5-20个字符</span><span style="color: #800000;">'</span><span style="color: #000000;">})) </span><span style="color: #008000;">#</span><span style="color: #008000;"> 使用自定义验证规则</span> phone = fields.CharField(validators=<span style="color: #000000;">[mobile_validate, ], error_messages</span>={<span style="color: #800000;">'</span><span style="color: #800000;">required</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">手机不能为空</span><span style="color: #800000;">'</span><span style="color: #000000;">}, widget</span>=widgets.TextInput(attrs={<span style="color: #800000;">'</span><span style="color: #800000;">class</span><span style="color: #800000;">'</span>: <span style="color: #800000;">"</span><span style="color: #800000;">form-control</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">placeholder</span><span style="color: #800000;">'</span>: u<span style="color: #800000;">'</span><span style="color: #800000;">手机号码</span><span style="color: #800000;">'</span><span style="color: #000000;">})) email </span>= fields.EmailField(required=<span style="color: #000000;">False, error_messages</span>={<span style="color: #800000;">'</span><span style="color: #800000;">required</span><span style="color: #800000;">'</span>: u<span style="color: #800000;">'</span><span style="color: #800000;">邮箱不能为空</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">invalid</span><span style="color: #800000;">'</span>: u<span style="color: #800000;">'</span><span style="color: #800000;">邮箱格式错误</span><span style="color: #800000;">'</span><span style="color: #000000;">}, widget</span>=widgets.TextInput(attrs={<span style="color: #800000;">'</span><span style="color: #800000;">class</span><span style="color: #800000;">'</span>: <span style="color: #800000;">"</span><span style="color: #800000;">form-control</span><span style="color: #800000;">"</span>, <span style="color: #800000;">'</span><span style="color: #800000;">placeholder</span><span style="color: #800000;">'</span>: u<span style="color: #800000;">'</span><span style="color: #800000;">邮箱</span><span style="color: #800000;">'</span>}))</pre>
除了上面两种方式,我们还可以在Form类中定义钩子函数,来实现自定义的验证功能。
局部钩子
我们在Fom类中定义 clean_字段名() 方法,就能够实现对特定字段进行校验。
举个例子:
class LoginForm(forms.Form): username = forms.CharField( min_length=8, label="用户名", initial="张三", error_messages={ "required": "不能为空", "invalid": "格式错误", "min_length": "用户名最短8位" }, widget=forms.widgets.TextInput(attrs={"class": "form-control"}) ) ... # 定义局部钩子,用来校验username字段,之前的校验股则还在,给你提供了一个添加一些校验功能的钩子 def clean_username(self): value = self.cleaned_data.get("username") if "666" in value: raise ValidationError("光喊666是不行的") else: return value
全局钩子
我们在Fom类中定义 clean() 方法,就能够实现对字段进行全局校验,字段全部验证完,局部钩子也全部执行完之后,执行这个全局钩子校验。
class LoginForm(forms.Form): ... password = forms.CharField( min_length=6, label="密码", widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'}, render_value=True) ) re_password = forms.CharField( min_length=6, label="确认密码", widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'}, render_value=True) ) ... # 定义全局的钩子,用来校验密码和确认密码字段是否相同,执行全局钩子的时候,cleaned_data里面肯定是有了通过前面验证的所有数据 def clean(self): password_value = self.cleaned_data.get('password') re_password_value = self.cleaned_data.get('re_password') if password_value == re_password_value: return self.cleaned_data #全局钩子要返回所有的数据 else: self.add_error('re_password', '两次密码不一致') #在re_password这个字段的错误列表中加上一个错误,并且clean_data里面会自动清除这个re_password的值,所以打印clean_data的时候会看不到它 raise ValidationError('两次密码不一致')
应用Bootstrap样式
Django form应用Bootstrap样式简单示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"> <title>login</title> </head> <body> <div class="container"> <div class="row"> <form action="/login2/" method="post" novalidate class="form-horizontal"> {% csrf_token %} <div class="form-group"> <label for="{{ form_obj.username.id_for_label }}" class="col-md-2 control-label">{{ form_obj.username.label }}</label> <div class="col-md-10"> {{ form_obj.username }} <span class="help-block">{{ form_obj.username.errors.0 }}</span> </div> </div> <div class="form-group"> <label for="{{ form_obj.pwd.id_for_label }}" class="col-md-2 control-label">{{ form_obj.pwd.label }}</label> <div class="col-md-10"> {{ form_obj.pwd }} <span class="help-block">{{ form_obj.pwd.errors.0 }}</span> </div> </div> <div class="form-group"> <label class="col-md-2 control-label">{{ form_obj.gender.label }}</label> <div class="col-md-10"> <div class="radio"> {% for radio in form_obj.gender %} <label for="{{ radio.id_for_label }}"> {{ radio.tag }}{{ radio.choice_label }} </label> {% endfor %} </div> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <button type="submit" class="btn btn-default">注册</button> </div> </div> </form> </div> </div><script src="/static/jquery-3.2.1.min.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
批量添加样式
可通过重写form类的init方法来实现。
class LoginForm(forms.Form): username = forms.CharField( min_length=8, label="用户名", initial="张三", error_messages={ "required": "不能为空", "invalid": "格式错误", "min_length": "用户名最短8位" } ...</span><span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span>(self, *args, **<span style="color: #000000;">kwargs): super(LoginForm, self).</span><span style="color: #800080;">__init__</span>(*args, **<span style="color: #000000;">kwargs) </span><span style="color: #0000ff;">for</span> field <span style="color: #0000ff;">in</span><span style="color: #000000;"> iter(self.fields): self.fields[field].widget.attrs.update({ </span><span style="color: #800000;">'</span><span style="color: #800000;">class</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">form-control</span><span style="color: #800000;">'</span><span style="color: #000000;"> })</span></pre>
简单写一个小项目:book表的添加和数据展示
views.py内容如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.shortcuts import render,HttpResponse,redirect from app01 import models from django import forms # Create your views here.class BookForm(forms.Form):
title = forms.CharField(
max_length=16,
label='书名:',
initial='zhang',
# widget = forms.widgets.PasswordInput(attrs={'class': 'form-control'},render_value=True),
widget = forms.widgets.TextInput(attrs={'class': 'form-control'},),) </span><span style="color: #008000;">#</span><span style="color: #008000;"> title2 = forms.CharField(</span> <span style="color: #008000;">#</span><span style="color: #008000;"> max_length=16,</span> <span style="color: #008000;">#</span><span style="color: #008000;"> label='书名:',</span> <span style="color: #008000;">#</span><span style="color: #008000;"> initial='zhang',</span> <span style="color: #008000;">#</span><span style="color: #008000;"> # widget = forms.widgets.PasswordInput(attrs={'class': 'form-control'},render_value=True),</span> <span style="color: #008000;">#</span><span style="color: #008000;"> widget=forms.widgets.TextInput(attrs={'class': 'form-control'}, ),</span> <span style="color: #008000;">#
# )
sex = forms.ChoiceField(
choices=(
('1','男'),
('2','女'),
)
,
label='性别:',
# widget=forms.widgets.RadioSelect(),
# widget=forms.widgets.RadioSelect(),
# widget=forms.widgets.SelectMultiple(attrs={'class':'form-control'}),
widget=forms.widgets.Select(attrs={'class':'form-control'}),
# widget=forms.widgets.CheckboxInput(),
# widget=forms.widgets.CheckboxSelectMultiple(),
)
publishDate = forms.DateField(
label='出版日期:',
widget=forms.widgets.TextInput(attrs={'type':'date','class':'form-control'}),
)
price=forms.DecimalField(
max_digits=5,
decimal_places=2,
label='书籍的价格:',
widget=forms.widgets.NumberInput(attrs={'class': 'form-control'}
))</span><span style="color: #008000;">#</span><span style="color: #008000;"> publish = forms.ModelChoiceField(</span> <span style="color: #008000;">#</span><span style="color: #008000;"> label='出版社:',</span> <span style="color: #008000;">#</span><span style="color: #008000;"> queryset=models.Publish.objects.all(),</span> <span style="color: #008000;">#</span><span style="color: #008000;"> widget=forms.widgets.Select(attrs={'class': 'form-control'}</span> <span style="color: #008000;">#</span><span style="color: #008000;"> ))</span> publish_id =<span style="color: #000000;"> forms.ChoiceField( label</span>=<span style="color: #800000;">'</span><span style="color: #800000;">出版社:</span><span style="color: #800000;">'</span><span style="color: #000000;">, widget</span>=forms.widgets.Select(attrs={<span style="color: #800000;">'</span><span style="color: #800000;">class</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">form-control</span><span style="color: #800000;">'</span><span style="color: #000000;">} )) authors </span>=<span style="color: #000000;"> forms.ModelMultipleChoiceField( queryset</span>=<span style="color: #000000;">models.Author.objects.all(), widget </span>= forms.widgets.SelectMultiple(attrs={<span style="color: #800000;">'</span><span style="color: #800000;">class</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">form-control</span><span style="color: #800000;">'</span><span style="color: #000000;">} )) </span><span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span>(self,*args, **<span style="color: #000000;">kwargs): super().</span><span style="color: #800080;">__init__</span>(*args, **<span style="color: #000000;">kwargs) self.fields[</span><span style="color: #800000;">'</span><span style="color: #800000;">publish_id</span><span style="color: #800000;">'</span>].choices = models.Publish.objects.values_list(<span style="color: #800000;">'</span><span style="color: #800000;">pk</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">name</span><span style="color: #800000;">'</span><span style="color: #000000;">)
def index(request):
if request.method == 'GET':
form_obj = BookForm()</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">index.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">form_obj</span><span style="color: #800000;">'</span><span style="color: #000000;">:form_obj}) </span><span style="color: #0000ff;">else</span><span style="color: #000000;">: </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(request.POST) form_obj </span>=<span style="color: #000000;"> BookForm(request.POST) </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(form_obj.is_valid()) </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(form_obj.errors) </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> form_obj.is_valid(): </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(form_obj.cleaned_data) authors_obj </span>= form_obj.cleaned_data.pop(<span style="color: #800000;">'</span><span style="color: #800000;">authors</span><span style="color: #800000;">'</span><span style="color: #000000;">) new_book_obj </span>= models.Book.objects.create(**<span style="color: #000000;">form_obj.cleaned_data) new_book_obj.authors.add(</span>*<span style="color: #000000;">authors_obj) </span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">'</span><span style="color: #800000;">show</span><span style="color: #800000;">'</span><span style="color: #000000;">) </span><span style="color: #0000ff;">else</span><span style="color: #000000;">: </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(form_obj.errors) </span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">index.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">form_obj</span><span style="color: #800000;">'</span><span style="color: #000000;">:form_obj})
def show(request):
book_objs </span>=<span style="color: #000000;"> models.Book.objects.all() </span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">show.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">book_objs</span><span style="color: #800000;">'</span><span style="color: #000000;">:book_objs})
def edit_book(request,n):
</span><span style="color: #0000ff;">return</span> HttpResponse(<span style="color: #800000;">'</span><span style="color: #800000;">欢迎来到编辑页面</span><span style="color: #800000;">'</span><span style="color: #000000;">)
def delete_book(request,n):
return HttpResponse('欢迎来到删除页面')
urls.py内容如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index,name='index'), url(r'^show/', views.show,name='show'), url(r'^edit_book/(\d+)/', views.edit_book,name='edit_book'), url(r'^delete_book/(\d+)/', views.delete_book,name='delete_book'), ]
index.html内容如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.0-dist/dist/css/bootstrap.min.css' %}"> </head> <body><h1>展示页面</h1>
{#<div>#}
{# {{ form_obj.as_p }}#}
{##}
{#</div>#}{#{% for field in form_obj %}#}
{# <div>#}
{# {{ field }}#}
{# </div>#}
{##}<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form action="" novalidate method="post">
{% for field in form_obj %}
<div class="form-group {% if field.errors.0 %}has-error{% endif %}">
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
<span class="text-danger">{{ field.errors.0 }}</span>
</div>{# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="{{ form_obj.sex.id_for_label }}"</span><span style="color: #0000ff;">></span>{{ form_obj.sex.label }}<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# {{ form_obj.sex }}#} {# {{ form_obj.sex.errors.0 }}#} {# </span><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="{{ form_obj.publishDate.id_for_label }}"</span><span style="color: #0000ff;">></span>{{ form_obj.publishDate.label }}<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# {{ form_obj.publishDate }}#} {# {{ form_obj.publishDate.errors.0 }}#} {# </span><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="{{ form_obj.price.id_for_label }}"</span><span style="color: #0000ff;">></span>{{ form_obj.price.label }}<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# {{ form_obj.price }}#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="text-danger"</span><span style="color: #0000ff;">></span>{{ form_obj.price.errors.0 }}<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="{{ form_obj.publish.id_for_label }}"</span><span style="color: #0000ff;">></span>{{ form_obj.publish.label }}<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# {{ form_obj.publish }}#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="text-danger"</span><span style="color: #0000ff;">></span>{{ form_obj.publish.errors.0 }}<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="{{ form_obj.authors.id_for_label }}"</span><span style="color: #0000ff;">></span>{{ form_obj.authors.label }}<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# {{ form_obj.authors }}#} {# </span><span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="text-danger"</span><span style="color: #0000ff;">></span>{{ form_obj.authors.errors.0 }}<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} {# </span><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #000000;">#} </span><span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
</div>
<div>
</div>
</body>
<script src="{% static 'bootstrap-3.3.0-dist/dist/jQuery/jquery-3.1.1.js' %}"></script>
<script src="{% static 'bootstrap-3.3.0-dist/dist/js/bootstrap.min.js' %}"></script>
</html>
show.html内容如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.0-dist/dist/css/bootstrap.min.css' %}"> </head> <body><h1>数据展示</h1>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<table class="table">
<thead>
<tr>
<th>id</th>
<th>title</th>
<th>性别</th>
<th>出版日期</th>
<th>出版社</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book_obj in book_objs %}
<tr>
<td>{{ book_obj.pk }}</td>
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.get_sex_display }}</td>
<td>{{ book_obj.publishDate|date:'Y-d-m' }}</td>
<td>{{ book_obj.publish.name }}</td>
<td>
{% for author in book_obj.authors.all %}
{{ author.name }}</span><span style="color: #0000ff;"></</span><span style="color: #800000;">td</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">td</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="btn btn-warning"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="{% url 'edit_book' book_obj.pk %}"</span><span style="color: #0000ff;">></span>编辑<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="btn btn-danger"</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">="{% url 'delete_book' book_obj.pk %}"</span><span style="color: #0000ff;">></span>删除<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">td</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {% endfor %} </span><span style="color: #0000ff;"></</span><span style="color: #800000;">tbody</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">table</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
</div>
</body>
<script src="{% static 'bootstrap-3.3.0-dist/dist/jQuery/jquery-3.1.1.js' %}"></script>
<script src="{% static 'bootstrap-3.3.0-dist/dist/js/bootstrap.min.js' %}"></script>
</html>
别忘了在settings.py中配置静态文件
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR,'statics'), ]
别忘了去下载一个bootstrap和jquery然后在上面的html文件中引入一下。
models.py文件内容如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.db import models# Create your models here.
from django.db import models
# Create your models here.
class Author(models.Model):
nid = models.AutoField(primary_key=True)
name=models.CharField( max_length=32)
age=models.IntegerField()
authorDetail=models.OneToOneField(to="AuthorDetail",to_field="nid")
def str(self):
return self.name
class AuthorDetail(models.Model):nid </span>= models.AutoField(primary_key=<span style="color: #000000;">True) birthday</span>=<span style="color: #000000;">models.DateField() telephone</span>=<span style="color: #000000;">models.BigIntegerField() addr</span>=models.CharField( max_length=64<span style="color: #000000;">)
class Publish(models.Model):
nid = models.AutoField(primary_key=True)
name=models.CharField( max_length=32)
city=models.CharField( max_length=32)
email=models.EmailField()
def str(self):
return self.nameclass Book(models.Model):
nid </span>= models.AutoField(primary_key=<span style="color: #000000;">True) title </span>= models.CharField( max_length=32<span style="color: #000000;">) sex_choice </span>=<span style="color: #000000;"> ( (</span>1,<span style="color: #800000;">'</span><span style="color: #800000;">男</span><span style="color: #800000;">'</span><span style="color: #000000;">), (</span>2,<span style="color: #800000;">'</span><span style="color: #800000;">女</span><span style="color: #800000;">'</span><span style="color: #000000;">), ) sex </span>= models.IntegerField(choices=sex_choice,default=1<span style="color: #000000;">) publishDate</span>=<span style="color: #000000;">models.DateField() price</span>=models.DecimalField(max_digits=5,decimal_places=2<span style="color: #000000;">) publish</span>=models.ForeignKey(to=<span style="color: #800000;">"</span><span style="color: #800000;">Publish</span><span style="color: #800000;">"</span>,to_field=<span style="color: #800000;">"</span><span style="color: #800000;">nid</span><span style="color: #800000;">"</span><span style="color: #000000;">) authors</span>=models.ManyToManyField(to=<span style="color: #800000;">'</span><span style="color: #800000;">Author</span><span style="color: #800000;">'</span><span style="color: #000000;">,) </span><span style="color: #0000ff;">def</span> <span style="color: #800080;">__str__</span><span style="color: #000000;">(self): </span><span style="color: #0000ff;">return</span> self.title</pre>
别忘了给每个表添加一些数据。
通常在Django项目中,我们编写的大部分都是与Django 的模型紧密映射的表单。 举个例子,你也许会有个Book 模型,并且你还想创建一个form表单用来添加和编辑书籍信息到这个模型中。 在这种情况下,在form表单中定义字段将是冗余的,因为我们已经在模型中定义了那些字段。
基于这个原因,Django 提供一个辅助类来让我们可以从Django 的模型创建Form,这就是ModelForm。
modelForm定义
form与model的终极结合,会根据你model中的字段转换成对应的form字段,并且并你生成标签等操作。
比如你的models中的表是下面的内容:
class Book(models.Model):nid </span>= models.AutoField(primary_key=<span style="color: #000000;">True) title </span>= models.CharField( max_length=32<span style="color: #000000;">) publishDate</span>=<span style="color: #000000;">models.DateField() price</span>=models.DecimalField(max_digits=5,decimal_places=2<span style="color: #000000;">) publish</span>=models.ForeignKey(to=<span style="color: #800000;">"</span><span style="color: #800000;">Publish</span><span style="color: #800000;">"</span>,to_field=<span style="color: #800000;">"</span><span style="color: #800000;">nid</span><span style="color: #800000;">"</span><span style="color: #000000;">) authors</span>=models.ManyToManyField(to=<span style="color: #800000;">'</span><span style="color: #800000;">Author</span><span style="color: #800000;">'</span><span style="color: #000000;">,) </span><span style="color: #0000ff;">def</span> <span style="color: #800080;">__str__</span><span style="color: #000000;">(self): </span><span style="color: #0000ff;">return</span> self.title</pre>
modelform类的写法。
class BookForm(forms.ModelForm):</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Meta: model </span>=<span style="color: #000000;"> models.Book fields </span>= <span style="color: #800000;">"</span><span style="color: #800000;">__all__</span><span style="color: #800000;">"</span><span style="color: #000000;"> labels </span>=<span style="color: #000000;"> { </span><span style="color: #800000;">"</span><span style="color: #800000;">title</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;">书名</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">price</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;">价格</span><span style="color: #800000;">"</span><span style="color: #000000;"> } widgets </span>=<span style="color: #000000;"> { </span><span style="color: #800000;">"</span><span style="color: #800000;">password</span><span style="color: #800000;">"</span>: forms.widgets.PasswordInput(attrs={<span style="color: #800000;">"</span><span style="color: #800000;">class</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;">c1</span><span style="color: #800000;">"</span><span style="color: #000000;">}),<br></span></pre>
"publishDate": forms.widgets.DateInput(attrs={"type": "date"}),}
class Meta下常用参数:
model = models.Book # 对应的Model中的类 fields = "__all__" # 字段,如果是__all__,就是表示列出所有的字段 exclude = None # 排除的字段 labels = None # 提示信息 help_texts = None # 帮助提示信息 widgets = None # 自定义插件 error_messages = None # 自定义错误信息
error_messages = {
'title':{'required':'不能为空',...} #每个字段的所有的错误都可以写,...是省略的意思,复制黏贴我代码的时候别忘了删了...
}
批量添加样式:和form的一样
class BookForm(forms.ModelForm):
#password = forms.CharField(min_length=10) #可以重写字段,会覆盖modelform中的这个字段,那么modelform下面关于这个字段的设置就会被覆盖,比如果设置插件啊,error_messages啊等等,
r_password = forms.CharField() #想多验证一些字段可以单独拿出来写,按照form的写法,写在Meta的上面或者下面都可以
class Meta: model = models.Book # fields = ['title','price'] fields = "__all__" #['title,'price'] 指定字段生成form # exclude=['title',] #排除字段 labels = { "title": "书名", "price": "价格" } error_messages = { 'title':{'required':'不能为空',} #每个字段的错误都可以写 } #如果models中的字段和咱们需要验证的字段对不齐的是,比如注册时,咱们需要验证密码和确认密码两个字段数据,但是后端数据库就保存一个数据就行,那么验证是两个,数据保存是一个,就可以再接着写form字段
r_password = forms.CharField()。
#同样的,如果想做一些特殊的验证定制,那么和form一昂,也是那两个钩子(全局和局部),写法也是form那个的写法,直接在咱们的类里面写:
#局部钩子:
def clean_title(self):
pass
#全局钩子
def clean(self):
pass
def __init__(self,*args,**kwargs): #批量操作 super().__init__(*args,**kwargs) for field in self.fields:
#field.error_messages = {'required':'不能为空'} #批量添加错误信息,这是都一样的错误,不一样的还是要单独写。 self.fields[field].widget.attrs.update({'class':'form-control'})
ModelForm的验证
与普通的Form表单验证类型类似,ModelForm表单的验证在调用is_valid() 或访问errors 属性时隐式调用。
我们可以像使用Form类一样自定义局部钩子方法和全局钩子方法来实现自定义的校验规则。
如果我们不重写具体字段并设置validators属性的话,ModelForm是按照模型中字段的validators来校验的。
save()方法
每个ModelForm还具有一个save()方法。 这个方法根据表单绑定的数据创建并保存数据库对象。 ModelForm的子类可以接受现有的模型实例作为关键字参数instance;如果提供此功能,则save()将更新该实例。 如果没有提供,save() 将创建模型的一个新实例:
>>> from myapp.models import Book >>> from myapp.forms import BookForm# 根据POST数据创建一个新的form对象
>>> form_obj = BookForm(request.POST)# 创建书籍对象
>>> new_ book = form_obj.save()# 基于一个书籍对象创建form对象
>>> edit_obj = Book.objects.get(id=1)
# 使用POST提交的数据更新书籍对象
>>> form_obj = BookForm(request.POST, instance=edit_obj)
>>> form_obj.save()
之前我们通过form组件来保存书籍表数据的时候的写法:
def index(request): if request.method == 'GET': form_obj = BookForm()</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">index.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">form_obj</span><span style="color: #800000;">'</span><span style="color: #000000;">:form_obj}) </span><span style="color: #0000ff;">else</span><span style="color: #000000;">: form_obj </span>=<span style="color: #000000;"> BookForm(request.POST) </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> form_obj.is_valid(): </span><span style="color: #008000;">#</span><span style="color: #008000;"> authors_obj = form_obj.cleaned_data.pop('authors')</span> <span style="color: #008000;">#</span><span style="color: #008000;"> new_book_obj = models.Book.objects.create(**form_obj.cleaned_data)</span> <span style="color: #008000;">#</span><span style="color: #008000;"> new_book_obj.authors.add(*authors_obj)</span> form_obj.save() <span style="color: #008000;">#</span><span style="color: #008000;">因为我们再Meta中指定了是哪张表,所以它会自动识别,不管是外键还是多对多等,都会自行处理保存,它完成的就是上面三句话做的事情,并且还有就是如果你验证的数据比你后端数据表中的字段多,那么他自会自动剔除多余的不需要保存的字段,比如那个重复确认密码就不要保存</span> <span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">'</span><span style="color: #800000;">show</span><span style="color: #800000;">'</span><span style="color: #000000;">) </span><span style="color: #0000ff;">else</span><span style="color: #000000;">: </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(form_obj.errors) </span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">index.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">form_obj</span><span style="color: #800000;">'</span>:form_obj})</pre>
比如说我们图书管理系统页面之前是这样写的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.0-dist/dist/css/bootstrap.min.css' %}"> </head> <body><h1>编辑页面</h1>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form action="">
<div class="form-group">
<label for="title">书名</label>
<input type="text" class="form-control" id="title" placeholder="title" value="{{ book_obj.title }}"><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="publishDate"</span><span style="color: #0000ff;">></span>出版日期<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="publishDate"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">="publishDate"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="{{ book_obj.publishDate|date:'Y-m-d' }}"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="price"</span><span style="color: #0000ff;">></span>价格<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="number"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="price"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">="price"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="{{ book_obj.price }}"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="publish"</span><span style="color: #0000ff;">></span>书名<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">select </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="publish"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="publish"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {% for publish in all_publish %} {% if publish == book_obj.publish %} </span><span style="color: #0000ff;"><</span><span style="color: #800000;">option </span><span style="color: #ff0000;">value</span><span style="color: #0000ff;">="{{ publish.id }}"</span><span style="color: #ff0000;"> selected</span><span style="color: #0000ff;">></span>{{ publish.name }}<span style="color: #0000ff;"></</span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {% else %} </span><span style="color: #0000ff;"><</span><span style="color: #800000;">option </span><span style="color: #ff0000;">value</span><span style="color: #0000ff;">="{{ publish.id }}"</span><span style="color: #0000ff;">></span>{{ publish.name }}<span style="color: #0000ff;"></</span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {% endif %} {% endfor %} </span><span style="color: #0000ff;"></</span><span style="color: #800000;">select</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="authors"</span><span style="color: #0000ff;">></span>书名<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">select </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="authors"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="authors"</span><span style="color: #ff0000;"> multiple class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {% for author in all_authors %} {% if author in book_obj.authors.all %} </span><span style="color: #0000ff;"><</span><span style="color: #800000;">option </span><span style="color: #ff0000;">value</span><span style="color: #0000ff;">="{{ author.id }}"</span><span style="color: #ff0000;"> selected</span><span style="color: #0000ff;">></span>{{ author.name }}<span style="color: #0000ff;"></</span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {% else %} </span><span style="color: #0000ff;"><</span><span style="color: #800000;">option </span><span style="color: #ff0000;">value</span><span style="color: #0000ff;">="{{ author.id }}"</span> <span style="color: #0000ff;">></span>{{ author.name }}<span style="color: #0000ff;"></</span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {% endif %} {% endfor %} </span><span style="color: #0000ff;"></</span><span style="color: #800000;">select</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
</div>
</body>
<script src="{% static 'bootstrap-3.3.0-dist/dist/jQuery/jquery-3.1.1.js' %}"></script>
<script src="{% static 'bootstrap-3.3.0-dist/dist/js/bootstrap.min.js' %}"></script>
</html>
views.py是这样写的:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def edit_book(request,n):book_obj </span>= models.Book.objects.filter(pk=<span style="color: #000000;">n).first() </span><span style="color: #0000ff;">if</span> request.method == <span style="color: #800000;">'</span><span style="color: #800000;">GET</span><span style="color: #800000;">'</span><span style="color: #000000;">: all_authors </span>= models.Author.objects.all() <span style="color: #008000;">#
all_publish = models.Publish.objects.all()
</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">edit_book.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">book_obj</span><span style="color: #800000;">'</span>:book_obj,<span style="color: #800000;">'</span><span style="color: #800000;">all_authors</span><span style="color: #800000;">'</span>:all_authors,<span style="color: #800000;">'</span><span style="color: #800000;">all_publish</span><span style="color: #800000;">'</span>:all_publish})</pre>
改成使用modelform之后,我们这样写:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.0-dist/dist/css/bootstrap.min.css' %}"> </head> <body><h1>编辑页面</h1>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form action="{% url 'edit_book' n %}" novalidate method="post">
{% csrf_token %}
{% for field in form %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
<span class="text-danger">{{ field.errors.0 }}</span>
</div></span><span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="form-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="btn btn-primary pull-right"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
</div>
</body>
<script src="{% static 'bootstrap-3.3.0-dist/dist/jQuery/jquery-3.1.1.js' %}"></script>
<script src="{% static 'bootstrap-3.3.0-dist/dist/js/bootstrap.min.js' %}"></script>
</html>
views.py这样写:
def edit_book(request,n):form = BookForm(instance=book_obj)book_obj </span>= models.Book.objects.filter(pk=<span style="color: #000000;">n).first() </span><span style="color: #0000ff;">if</span> request.method == <span style="color: #800000;">'</span><span style="color: #800000;">GET</span><span style="color: #800000;">'</span><span style="color: #000000;">: </span><span style="color: #008000;">#</span><span style="color: #008000;"> all_authors = models.Author.objects.all() #</span> <span style="color: #008000;">#</span><span style="color: #008000;"> all_publish = models.Publish.objects.all()</span>
</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">edit_book.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">form</span><span style="color: #800000;">'</span>:form,<span style="color: #800000;">'</span><span style="color: #800000;">n</span><span style="color: #800000;">'</span>:n}) <span style="color: #008000;">#</span><span style="color: #008000;">传递的这个n参数是给form表单提交数据的是的action的url用的,因为它需要一个参数来识别是更新的哪条记录</span> <span style="color: #0000ff;">else</span><span style="color: #000000;">: form </span>= BookForm(request.POST,instance=book_obj) <span style="color: #008000;">#</span><span style="color: #008000;">必须指定instance,不然我们调用save方法的是又变成了添加操作</span> <span style="color: #0000ff;">if</span><span style="color: #000000;"> form.is_valid(): form.save() </span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">'</span><span style="color: #800000;">show</span><span style="color: #800000;">'</span><span style="color: #000000;">) </span><span style="color: #0000ff;">else</span><span style="color: #000000;">: </span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">edit_book.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">form</span><span style="color: #800000;">'</span>:form,<span style="color: #800000;">'</span><span style="color: #800000;">n</span><span style="color: #800000;">'</span>:n})</pre>
这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来,先来一个简单的例子来看一下这个东西怎么用:比如我们的数据库中有这样一张学生表,字段有姓名,年龄,爱好,邮箱,电话,住址,注册时间等等一大堆信息,现在让你写一个创建学生的页面,你的后台应该怎么写呢?首先我们会在前端一个一个罗列出这些字段,让用户去填写,然后我们从后天一个一个接收用户的输入,创建一个新的学生对象,保存其实,重点不是这些,而是合法性验证,我们需要在前端判断用户输入是否合法,比如姓名必须在多少字符以内,电话号码必须是多少位的数字,邮箱必须是邮箱的格式这些当然可以一点一点手动写限制,各种判断,这毫无问题,除了麻烦我们现在有个更优雅(以后在Python相关的内容里,要多用“优雅”这个词,并且养成习惯)的方法:ModelForm先来简单的,生硬的把它用上,再来加验证条件。
创建modelform
#首先导入ModelFormfrom django.forms import ModelForm
在视图函数中,定义一个类,比如就叫StudentList,这个类要继承ModelForm,在这个类中再写一个原类Meta(规定写法,并注意首字母是大写的)
在这个原类中,有以下属性(部分):
class StudentList(ModelForm):
class Meta:
model =Student #对应的Model中的类
fields = "all" #字段,如果是__all__,就是表示列出所有的字段
exclude = None #排除的字段
#error_messages用法:
error_messages = {
'name':{'required':"用户名不能为空",},
'age':{'required':"年龄不能为空",},
}
#widgets用法,比如把输入用户名的input框给为Textarea
#首先得导入模块
from django.forms import widgets as wid #因为重名,所以起个别名
widgets = {
"name":wid.Textarea(attrs={"class":"c1"}) #还可以自定义属性
}
#labels,自定义在前端显示的名字
labels= {
"name":"用户名"
}
然后在url对应的视图函数中实例化这个类,把这个对象传给前端
def student(request):if request.method == 'GET': student_list = StudentList() return render(request,'student.html',{'student_list':student_list}) </pre>
然后前端只需要 {{ student_list.as_p }} 一下,所有的字段就都出来了,可以用as_p显示全部,也可以通过for循环这
student_list,拿到的是一个个input框,现在我们就不用as_p,手动把这些input框搞出来,as_p拿到的页面太丑。
首先 for循环这个student_list,拿到student对象,直接在前端打印这个student,是个input框student.label ,拿到数据库中每个字段的verbose_name ,如果没有设置这个属性,拿到的默认就是字段名,还可以通过student.errors.0 拿到错误信息有了这些,我们就可以通过bootstrap,自己拼出来想要的样式了,比如:
<body> <div class="container"> <h1>student</h1> <form method="POST" novalidate> {% csrf_token %} {# {{ student_list.as_p }}#} {% for student in student_list %} <div class="form-group col-md-6"> {# 拿到数据字段的verbose_name,没有就默认显示字段名 #} <label class="col-md-3 control-label">{{ student.label }}</label> <div class="col-md-9" style="position: relative;">{{ student }}</div> </div> {% endfor %} <div class="col-md-2 col-md-offset-10"> <input type="submit" value="提交" class="btn-primary"> </div> </form> </div> </body>
现在还缺一个input框的form-contral样式,可以考虑在后台的widget里面添加
比如这样:
from django.forms import widgets as wid #因为重名,所以起个别名 widgets = { "name":wid.TextInput(attrs={'class':'form-control'}), "age":wid.NumberInput(attrs={'class':'form-control'}), "email":wid.EmailInput(attrs={'class':'form-control'}) }
当然也可以在js中,找到所有的input框,加上这个样式,也行。
添加纪录
保存数据的时候,不用挨个取数据了,只需要save一下
def student(request):if request.method == 'GET': student_list = StudentList() return render(request,'student.html',{'student_list':student_list}) else: student_list = StudentList(request.POST) if student_list.is_valid(): student_list.save() return redirect(request,'student_list.html',{'student_list':student_list})</pre>
编辑数据
如果不用ModelForm,编辑的时候得显示之前的数据吧,还得挨个取一遍值,如果ModelForm,只需要加一个instance=obj(obj是要修改的数据库的一条数据的对象)就可以得到同样的效果
保存的时候要注意,一定要注意有这个对象(instance=obj),否则不知道更新哪一个数据
代码示例:
from django.shortcuts import render,HttpResponse,redirect from django.forms import ModelForm # Create your views here. from app01 import models def test(request): # model_form = models.Student model_form = models.Student.objects.all() return render(request,'test.html',{'model_form':model_form})class StudentList(ModelForm):
class Meta:
model = models.Student #对应的Model中的类
fields = "all" #字段,如果是__all__,就是表示列出所有的字段
exclude = None #排除的字段
labels = None #提示信息
help_texts = None #帮助提示信息
widgets = None #自定义插件
error_messages = None #自定义错误信息
#error_messages用法:
error_messages = {
'name':{'required':"用户名不能为空",},
'age':{'required':"年龄不能为空",},
}
#widgets用法,比如把输入用户名的input框给为Textarea
#首先得导入模块
from django.forms import widgets as wid #因为重名,所以起个别名
widgets = {
"name":wid.Textarea
}
#labels,自定义在前端显示的名字
labels= {
"name":"用户名"
}
def student(request):
if request.method == 'GET':
student_list = StudentList()
return render(request,'student.html',{'student_list':student_list})
else:
student_list = StudentList(request.POST)
if student_list.is_valid():
student_list.save()
return render(request,'student.html',{'student_list':student_list})def student_edit(request,pk):
obj = models.Student.objects.filter(pk=pk).first()
if not obj:
return redirect('test')
if request.method == "GET":
student_list = StudentList(instance=obj)
return render(request,'student_edit.html',{'student_list':student_list})
else:
student_list = StudentList(request.POST,instance=obj)
if student_list.is_valid():
student_list.save()
return render(request,'student_edit.html',{'student_list':student_list})
总结: 从上边可以看到ModelForm用起来是非常方便的,比如增加修改之类的操作。但是也带来额外不好的地方,model和form之间耦合了。如果不耦合的话,mf.save()方法也无法直接提交保存。 但是耦合的话使用场景通常局限用于小程序,写大程序就最好不用了。
0-9 ↩︎