22 BootStrapModelForm
方便之处在于,我们不会再一遍一遍的写form的样式了。
from django import forms
class BootStrapModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BootStrapModelForm, self).__init__(*args, **kwargs)
# 统一给ModelForm生成字段添加样式
for name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
如果还想自定义一些字段不添加样式,那么可以这样写:
class BootStrapModelForm:
exclude_filed_list = []
def __init__(self, *args, **kwargs):
super(BootStrapModelForm, self).__init__(*args, **kwargs)
# 统一给ModelForm生成字段添加样式
for name, field in self.fields.items():
if name in self.exclude_filed_list:
continue
field.widget.attrs['class'] = 'form-control'
class CustomerModelForm(BootStrapModelForm, forms.ModelForm):
exclude_filed_list = ['level'] # 不需要添加样式的字段名
思考:无论在使用Form和ModelForm时,想要让页面好看,就需要将每个字段的插件中给他设置form-control样式。
class LevelForm(forms.Form):
title = forms.CharField(
label="标题",
required=True,
# widget=forms.TextInput(attrs={"class": "form-control", 'placeholder': "请输入标题"}),
)
percent = forms.CharField(
label="折扣",
required=True,
help_text="填入0-100整数表示百分比,例如:90,表示90%"
)
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
# {'title':对象,"percent":对象}
for name,field in self.fields.items():
field.widget.attrs['class'] = "form-control"
field.widget.attrs['placeholder'] = "请输入{}".format(field.label)
class LevelModelForm(forms.ModelForm):
class Meta:
model = models.Level
fields = ['title', 'percent']
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
# {'title':对象,"percent":对象}
for name,field in self.fields.items():
field.widget.attrs['class'] = "form-control"
field.widget.attrs['placeholder'] = "请输入{}".format(field.label)
22 BootStrapModelForm的更多相关文章
- CENTOS 6.5 平台离线编译安装 Mysql5.6.22
一.下载源码包 http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.22.tar.gz 二.准备工作 卸载之前本机自带的MYSQL 安装 cmake,编 ...
- EC笔记:第4部分:22、所有成员都应该是private的
EC笔记:第4部分:22.所有成员都应该是private的 更简单的访问 用户不用记得什么时候该带上括号,什么时候不用带上括号(因为很确定的就要带上括号) 访问限制 对于public的成员变量,我们可 ...
- Hadoop学习笔记—22.Hadoop2.x环境搭建与配置
自从2015年花了2个多月时间把Hadoop1.x的学习教程学习了一遍,对Hadoop这个神奇的小象有了一个初步的了解,还对每次学习的内容进行了总结,也形成了我的一个博文系列<Hadoop学习笔 ...
- 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验
在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...
- Fedora 22中的Services and Daemons
Introduction Maintaining security on your system is extremely important, and one approach for this t ...
- Fedora 22中的RPM软件包管理工具
Introduction The RPM Package Manager (RPM) is an open packaging system that runs on Fedora as well a ...
- Fedora 22中的用户和用户组管理
The control of users and groups is a core element of Fedora system administration. This chapter expl ...
- Fedora 22中的日期和时间配置
Introduction Modern operating systems distinguish between the following two types of clocks: A real- ...
- Fedora 22中的DNF软件包管理工具
Introduction DNF is the The Fedora Project package manager that is able to query for information abo ...
- CSharpGL(22)实现顺序无关的半透明渲染(Order-Independent-Transparency)
+BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(22)实现顺序无关的半透明渲染(Order-Independent-Transparency) 在 GL.Enable(GL_BLEND ...
随机推荐
- PHP-循环文件夹-yield-生成器
<?php define(DS, DIRECTORY_SEPARATOR); function gen_dir($path='.'){ $c_dir = opendir($path); whil ...
- .NET Core基础:白话管道中间件
在Asp.Net Core中,管道往往伴随着请求一起出现.客户端发起Http请求,服务端去响应这个请求,之间的过程都在管道内进行. 举一个生活中比较常见的例子:旅游景区. 我们都知道,有些景区大门离景 ...
- springMVC学习day02
了解springMVC 1. 了解官网 1. 首先到spring地址去,然后选择项目下面的任何一个子项目,我选择spring framework https://spring.io/ 2.选择spr ...
- (03-14) synopsys中工具介绍,VCS,DC,PT等
https://blog.csdn.net/fangxiangeng/article/details/80981536 (1)Nlint 检查,spyglass (2)PT 静态时序检查 (3)Icc ...
- 2018GPLT
2018GPLT 7-1 天梯赛座位分配 一共有n所学校参加比赛,每所学校有\(a_i\)只队伍,每只队伍共10人,要保证每个学校的所有队员不能相邻就坐,令每一所学校的队伍排成一排纵列,然后从第一所学 ...
- Delphi 从字符串中提取数字
function GetNumberFromStr(strIn: string; sFlag: string): string; var i: Integer; tempStr: string; be ...
- 读取数组树下的某值,并返回其父级下的任何值 vue
1 // 遍历树 获取对应 id的项中的值 2 queryTree(tree, value) { 3 let stark = []; 4 stark = stark.concat(tree); 5 w ...
- 解决504 GATEWAY TIMEOUT Nginx网关超时
最近遇到一个问题504 GATEWAY TIMEOUT的问题,在浏览器的NetWork里面看是这个效果.时间大概是60s. 修改nginx配置 目前后端技术架构主要是nginx和php-fpm,前端主 ...
- python翻译(二)
百度翻译,通过设置参数可可以实现翻译为指定语种 baidujs1.js var i = "320305.131321201" function n(r, o) { for (var ...
- qt 单元测试遇到的问题
升级了qt creator到新版本.使用 google 单元测试,发现一个单元测试显示不对了. 测试结果输出:FATAL, 项目"t_ps"的测试未产生任何预期输出 比较了几个测试 ...