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 ...
随机推荐
- appium遇到的问题
问题1:权限问题:java.lang.SecurityException: Injecting to another application requires INJECT_EV ENTS permi ...
- temp_laijx_2023
############################ [{\"itemKey\": \"jenkinsConfig\",\"itemValue\& ...
- (1127)arm 架构, c++模板
(1)ARM M0 (2)c++形参
- Systrace学习记录
「置顶」Android 性能优化必知必会[大量文章] https://androidperformance.com/2018/05/07/Android-performance-optimizatio ...
- win10系统解决启动虚拟机蓝屏问题
1.查看电脑是否开启了虚拟化,如果没起开开启 CTR+SHIFT+Esc 2.是否开启了虚拟化选项 win+r 输入 OptionalFeatures
- [转载]OpenCV中的channel是什么意思?
转载自https://answers.opencv.org/question/7585/meaning-of-channels/ 简单来说,就是描述一个pixel的颜色用多少个独立的参数描述,这个个数 ...
- Oracle EBS 查看关联关系
打比方说,如何知道这个视图/表/包/trigger 在其他包/视图等引用. 1. select * from all_dependencies a where a.REFERENCED_NAME = ...
- OTP: gen_server的简单应用
1.确定回调模块名 2.编写接口函数 3.在回调模块里编写六个必需的回调函数 1.确定回调模块名 my_bank 2.编写接口方法 start() 打开银行 stop() 关闭银行 new_accou ...
- js常用代码整理2
引用js <script type="text/javascript" src="js/jquery-1.11.2.min.js"></scr ...
- 《计算机是怎么跑起来的》第十章 XML(可扩展标记语言)
资料来源 (1) <计算机是怎么跑起来的> 注1:XML是Extensible Markup Language(可扩展标记语言)的缩写; 1.XML是标记语言 (1) 通常把通过添加标签为 ...