方便之处在于,我们不会再一遍一遍的写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. python+接口参数化(ddt和pytest.mark.parametrize())使用

    一.ddt(基于unittest) 实例:字典解包[{},{}] test_data=t.read_excel(mode,case_list)@ddt class Interface(unittest ...

  2. element-ui学习之-------input表单验证【各种情况总结】

    1.非必填,正整数 2.非0开头正整数

  3. map转换 bean和list

    转实体类 JSONObject JSONObj = JSONObject.parseObject(JSON.toJSONString(resultMap));String result = JSONO ...

  4. env_config

    import sys import os root_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__f ...

  5. [Nginx]status:203 Failed to start The NGINX HTTP and reverse proxy server

    怎么感觉Linux的nGinx比Win的事一个一个一个的多啊(半恼) 运行systemctl status nginx时提示: ① Process: 123456 ExecStartPre=/usr/ ...

  6. locust中的监听器

    locust的master相关的几个监听器: 心跳监听器: 一个while循环,不断判断所有client当前的心跳状况,如果有一个client失去了心跳,就打印了一个警告日志,如果所有client都失 ...

  7. 日常开发记录-js的Date对象中的toLocaleDateString()

    就是把Date对象的日期部分转换为字符串,并返回结果. 代码示例: console.log(new Date()) // 2023-01-10T05:42:41.926Z console.log(ne ...

  8. fetchAll 的小小分析

    includes\database\prefetch.inc line 425 $this->defaultFetchStyle: fetch_object int 5protected $de ...

  9. BigDecimal精度等注意事项

    1.BigDecimal运算时尽量传入字符串, 反例: BigDecimal num=new BigDecimal(75); num.multiply(new BigDecimal(0.5)).set ...

  10. STM32F0 LL库IIC第二地址配置错误

    最近在做F0项目要用到多个IIC地址,使用Cube生成的LL库,第二地址进不了中断 F0版本:1.11.0 STM32CubeX生成的LL库代码为: 1 static void MX_I2C1_Ini ...