方便之处在于,我们不会再一遍一遍的写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的更多相关文章

  1. CENTOS 6.5 平台离线编译安装 Mysql5.6.22

    一.下载源码包 http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.22.tar.gz 二.准备工作 卸载之前本机自带的MYSQL 安装 cmake,编 ...

  2. EC笔记:第4部分:22、所有成员都应该是private的

    EC笔记:第4部分:22.所有成员都应该是private的 更简单的访问 用户不用记得什么时候该带上括号,什么时候不用带上括号(因为很确定的就要带上括号) 访问限制 对于public的成员变量,我们可 ...

  3. Hadoop学习笔记—22.Hadoop2.x环境搭建与配置

    自从2015年花了2个多月时间把Hadoop1.x的学习教程学习了一遍,对Hadoop这个神奇的小象有了一个初步的了解,还对每次学习的内容进行了总结,也形成了我的一个博文系列<Hadoop学习笔 ...

  4. 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验

    在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...

  5. Fedora 22中的Services and Daemons

    Introduction Maintaining security on your system is extremely important, and one approach for this t ...

  6. Fedora 22中的RPM软件包管理工具

    Introduction The RPM Package Manager (RPM) is an open packaging system that runs on Fedora as well a ...

  7. Fedora 22中的用户和用户组管理

    The control of users and groups is a core element of Fedora system administration. This chapter expl ...

  8. Fedora 22中的日期和时间配置

    Introduction Modern operating systems distinguish between the following two types of clocks: A real- ...

  9. Fedora 22中的DNF软件包管理工具

    Introduction DNF is the The Fedora Project package manager that is able to query for information abo ...

  10. CSharpGL(22)实现顺序无关的半透明渲染(Order-Independent-Transparency)

    +BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(22)实现顺序无关的半透明渲染(Order-Independent-Transparency) 在 GL.Enable(GL_BLEND ...

随机推荐

  1. vs MFC c++ rc文件的dilog打不开,显示加载失败

    最近在做一个项目,在vs2012上面用mfc画界面,由于没有确定好且不熟悉这个架构,控件类型变了又变,本人又是个有点强迫症的性格所以删掉了一些不用的控件,导致的结果就是上面说的,dialog打不开报加 ...

  2. (原创)Onlyoffice docs docker实例安装示例后,打开文档提示:文档下载失败

    不能使用localhost.请用本机ip访问

  3. File.Exists 判断不了虚拟路径

    https://www.shuzhiduo.com/topic/file-exists-%E5%88%A4%E6%96%AD%E4%B8%8D%E4%BA%86%E8%99%9A%E6%8B%9F%E ...

  4. OM6621P系列国产M4F内核低功耗BLE5.1 SoC蓝牙芯片

    随着5G与物联网时代到来,智慧城市.电动出行.智能家居.可穿戴设备等应用高速发展,低功耗蓝牙技术在近几年智能化浪潮中的地位也尤为重要.OM6621P系列的开发即是为解决国内低功耗蓝牙应用设计需求,其主 ...

  5. 【运维】通过gotty实现网页代理访问服务器及K8S容器操作实践

    Gotty 是Golang编写的可以方便的共享系统终端为web应用,是一个灵活强大的通过web访问终端的工具.本文将主要通过搭建Gotty实现对K8S容器的访问操作,开发如果想要正常的进行容器访问以及 ...

  6. django限制表单上传图片的大小

    django的ImageField没有提供控制上传图片的内置方法,我们可以在表单验证的过程中用clean函数来控制,搬运博客园 python小童鞋 ,可以通过重写ImageField的方法来控制上传图 ...

  7. CSS 多栏布局 固定布局 流动布局

    正常情况下都应该保持元素 height 属性的默认值 auto  . 多栏布局,某一栏目占的总宽度包括它的,Width,margin,padding ,border. CSS3中,应用 box-siz ...

  8. HDLbits——Exams/2014 q4b

    题目要求 使用verilog描述如图所示得移位寄存器: Write a top-level Verilog module (named top_module) for the shift regist ...

  9. Vue的v-html指令说明,含案例

    v-html指令总结: 1.作用:向指定节点中渲染包含html结构的内容 2.与插值语法的区别: (1)v-html 会替换掉节点中所有的内容,{{}}则不会 (2)v-html 可以识别html 结 ...

  10. 移动端性能测试--CPU资源

    一.背景 在很多场景下我们去使用 App,可能会碰到手机会出现发热发烫的现象.这是因为 CPU 使用率过高.CPU 过于繁忙,会使得整个系统无法响应用户,整体性能降低,用户体验变得相当差,也容易引起 ...