方便之处在于,我们不会再一遍一遍的写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. (已解决)nginx+php 上传文件大小设置。

    1. 问题 上传文件过大报错,413 Request Entity Too Large. 2. 解决方法 修改配置文件,在以下两处: a. php.ini文件(php目录) 在File Uploads ...

  2. Linux内核红黑树2—移植笔记

    转自:https://www.cnblogs.com/hellokitty2/p/15362596.html 另外可参考:https://zhuanlan.zhihu.com/p/26599934 一 ...

  3. Quartz 2D CGGradient与CGShading实现渐变的绘制

    Quartz 提供了两种不透明的数据类型来创建渐变CGShading 和 CGGradient,你可以使用其中任何一个来创建轴向或径向渐变. 轴向渐变:沿着一个轴方向线性渐变 径向渐变:一个点为原型, ...

  4. @Configuration 配置类打断点后,一启动项目读取到该配置类的话就会进断点

    @Configuration 配置类的话,打断点的时候,一启动项目就会读取配置信息,然后你在@Configuration 配置的类中打断点的话,一启动项目就会读取配置类,然后就会进断点,跟你平常的co ...

  5. shell 脚本case

    #! /bin/bash case $1 in 1) **** ;; 2) **** ;; 3) **** ;; esac

  6. MATLAB基础—基础认识

    数建-MATLAB(基础认识) 一些基本使用 clear all :清除Workspace中的所有变量 clc: 清除Command Window中的所有命令 注释:%%(空空格)多行注释 或 % 单 ...

  7. linux环境"ModuleNotFoundError: No module named 'Cryptodome'"

    linux环境"ModuleNotFoundError: No module named 'Cryptodome'" pip3 uninstall pycryptodome pip ...

  8. 学习-自增id++的问题

    代码示例: let id = 0   const todos = ref([   { id: id++, text: 'Learn HTML' },   { id: id++, text: 'Lear ...

  9. 【C和指针】6.指针

    1.指针变量的内容 int a=112, b=-1; float c=3.14; int *d=&a; int *e=&c; (1) 变量d和e被声明为指针,并用其他变量的地址予以初始 ...

  10. @Component类相互引用的加载顺序

    发现bug:没有消息通知,看日志发现调用消息通知的url前缀为null,定位到此工具类 进入工具类 进入ComponentConstant类:它引用了两个配置类 问题:component标注的类相互引 ...