在Django中无论何种field,都有一个widget的属性:

class Field(object):
widget = TextInput # Default widget to use when rendering this type of Field.
hidden_widget = HiddenInput # Default widget to use when rendering this as "hidden".

如上所示,widget默认是TextInput。

而TextInput是一个继承Input的一个类:

class TextInput(Input):
input_type = 'text' def __init__(self, attrs=None):
if attrs is not None:
self.input_type = attrs.pop('type', self.input_type)
super(TextInput, self).__init__(attrs)

往上接着找,Input继承于Widget,不同于Widget的是它有个input_type。当继承Input时需要指明input_type,像上面的TextInput一样指明为'text',类似的,PasswordInput则将其指明为'password'。

class Widget(six.with_metaclass(RenameWidgetMethods)):
needs_multipart_form = False # Determines does this widget need multipart form
is_localized = False
is_required = False
supports_microseconds = True def __init__(self, attrs=None):
if attrs is not None:
self.attrs = attrs.copy()
else:
self.attrs = {}

在Widget中有个attrs的属性,这个属性其实可以用来设置对应field的html属性。这个属性在Widget的render里会被引用。

所以如果你需要设置某个字段的html属性时,可以这么做:

field = forms.CharField()
field.widget.attrs['readonly']='true'

如果是一个这样的form:

from user.models import User
from django import forms class RegisterForm(forms.ModelForm): class Meta:
fields = ('first_name', 'last_name', 'email', 'username',)

你需要这样才能设置field的attrs:

for field in form:
field.field.widget.attrs['readonly']='false'

为什么要这样呢?因为ModelForm的Field是一个BoundField:

class BoundField(object):
"A Field plus data"
def __init__(self, form, field, name):
self.form = form
self.field = field
self.name = name
self.html_name = form.add_prefix(name)
self.html_initial_name = form.add_initial_prefix(name)
self.html_initial_id = form.add_initial_prefix(self.auto_id)
if self.field.label is None:
self.label = pretty_name(name)
else:
self.label = self.field.label
self.help_text = field.help_text or ''
self._initial_value = UNSET

BoundField不像CharField或其他一般的Field,并不是直接继承于Field。所以当你需要获得BoundField的Field的对象的时候,需要使用它的field的属性。

Django中的form设置field的html属性的更多相关文章

  1. Django中的form组件

    Django中的form组件有两大作用 1.验证获取正确的结果或者错误信息 2.生成html代码 一.为什么需要form组件呢? 在写form表单,提交数据时,自己写验证的代码是一件非常困难的事情. ...

  2. Django中的Form

    Form 一.使用Form Django中的Form使用时一般有两种功能: 1.生成html标签 2.验证输入内容 要想使用django提供的form,要在views里导入form模块 from dj ...

  3. Django中的Form表单

    Django中已经定义好了form类,可以很容易的使用Django生成一个表单. 一.利用Django生成一个表单: 1.在应用下创建一个forms文件,用于存放form表单.然后在forms中实例华 ...

  4. 转 Django中的Form

    https://www.cnblogs.com/chenchao1990/p/5284237.html Form 一.使用Form Django中的Form使用时一般有两种功能: 1.生成html标签 ...

  5. django中的 form 表单操作

     form组件  1. 能做什么事?   1. 能生成HTML代码  input框   2. 可以校验数据   3. 保留输入的数据   4. 有错误的提示   1. 定义   from django ...

  6. Django中的Form表单验证

    回忆一下Form表单验证的逻辑: 前端有若干个input输入框,将用户输入内容,以字典传递给后端. 后端预先存在一个Form表单验证的基类,封装了一个检测用户输入是否全部通过的方法.该方法会先定义好错 ...

  7. django中mysql数据库设置错误解决方法

    刚在django中settings.py进行设置mysql数据库. 当进行执行python manage.py shell命令时会报以下错误: 只需要在settings.py中 DATABASES = ...

  8. Django 中文和时区设置

    Django 语言和时区的设置都在 settings.py 文件中. 中文设置 LANGUAGE_CODE:设置语言,英语 en-us,中文简体 zh-Hans,中文繁体 zh-Hant 在 MIDD ...

  9. Django 中的Form、ModelForm

    一.ModelForm 源码 class ModelForm(BaseModelForm, metaclass=ModelFormMetaclass): pass def modelform_fact ...

随机推荐

  1. poj 3258 跳房子问题 最大化最小值

    题意:奶牛跳房子,从n块石头中移除M块,使得间距最小的最大值?思路:“转换” 从N块中选择n-m块使得两两之间的间距尽可能大 c(d) 是间距最大的满足条件,即第一块 放在 xi的位置 下一块就要放在 ...

  2. Appium运行时没有启动activity的权限:A new session could not be created.(Original error: Permission to start activity denied)

    小白搞appium,遇到启动不了activity的问题: 查找解决方案说是跟AndroidManifest.xml有关系,参考:https://github.com/appium/appium/iss ...

  3. django的as_view方法实现分析

    django的类视图拥有自动查找指定方法的功能, 通过调用是通过as_view()方法实现 urls.py from meduo_mall.demo import views urlpatterns ...

  4. luogu3195 [HNOI2008]玩具装箱TOY

    懒得写 #include <iostream> #include <cstdio> using namespace std; typedef long long longliv ...

  5. Migrate a Domain-based Namespace to Windows Server 2008 Mode

    TechNet Library Scripting with Windows PowerShell Windows and Windows Server Automation with Windows ...

  6. ROM+VGA 图片显示

    内容 1.将一幅图片制成mif文件,初始化rom,图片像素为 120 * 60 2.驱动VGA,将图片显示在屏幕上 1.VGA 时序 下面是我的笔记截图,感觉更好理解. 2.640*480 60hz ...

  7. Leetcode 630.课程表III

    课程表III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d天时要完成,你将会从第 ...

  8. 【转载】主成分分析法(PCA)

    https://www.jisilu.cn/question/252942 进行维数约减(Dimensionality Reduction),目前最常用的算法是主成分分析法 (Principal Co ...

  9. c#中dynamic ExpandoObject的用法

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. Selenium - WebDriver: Page Objects

    This chapter is a tutorial introduction to page objects design pattern. A page object represents an ...