field.widget.attrs = {"disabled" : True}这句话在HTML中怎么写
时间: 2024-03-28 13:42:40 浏览: 24
在HTML中,可以使用以下代码实现相同的效果:
```html
<input type="text" name="field_name" value="field_value" disabled>
```
其中,`field_name` 和 `field_value` 分别代表表单字段的名称和值。`disabled` 属性表示该表单字段为只读,不能被编辑。
相关问题
class BootStrap: bootstrap_exclude_fields = [] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # 循环ModelForm中的所有字段,给每个字段的插件设置 for name, field in self.fields.items(): if name in self.bootstrap_exclude_fields: continue # 字段中有属性,保留原来的属性,没有属性,才增加。 if field.widget.attrs: field.widget.attrs["class"] = "form-control" field.widget.attrs["placeholder"] = field.label else: field.widget.attrs = { "class": "form-control", "placeholder": field.label }
这是一个使用 Bootstrap 样式的 Django ModelForm 类的示例。其中,`bootstrap_exclude_fields` 属性用于指定需要排除的字段,不设置 Bootstrap 样式。在 `__init__` 方法中,通过遍历所有字段,设置每个字段的插件,即将其 `attrs` 属性中的 `class` 和 `placeholder` 设置为 Bootstrap 样式和输入提示。如果字段的 `attrs` 属性已经存在,那么将会保留原来的属性,否则会新增属性。这样,生成的表单页面就可以使用 Bootstrap 样式进行美化了。
class MyUserCreationForm(UserCreationForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['password1'].widget = forms.PasswordInput( attrs={'class': 'txt tabInput', 'placeholder': '密码,4-16位数字/字母/符号(空格除外)'}) self.fields['password2'].widget = forms.PasswordInput(attrs={'class': 'txt tabInput', 'placeholder': '重复密码'}) class Meta(UserCreationForm.Meta): model = MyUser fields = UserCreationForm.Meta.fields + ('mobile',) widgets = { 'mobile': forms.widgets.TextInput(attrs={'class': 'txt tabInput', 'placeholder': '手机号'}), 'username': forms.widgets.TextInput(attrs={'class': 'txt tabInput', 'placeholder': '用户名'}), }
这是一个继承自Django内置的UserCreationForm表单类的自定义表单类,用于创建新用户。该自定义表单类重写了__init__方法和Meta类。
__init__方法中通过调用父类的构造方法,继承了父类的所有字段和方法,并对其中的password1和password2字段的widget属性进行了重置,将其替换成了一个密码输入框,并添加了一些HTML属性,如class和placeholder等。
Meta类中重写了model属性和fields属性,指定了自定义用户模型MyUser和表单的字段。在这里,新增了mobile字段,并将其添加到UserCreationForm.Meta.fields元组中。同时,使用widgets属性指定了mobile和username字段的HTML属性,如class和placeholder等。
这个自定义表单类可以在创建新用户时使用。通过继承父类的所有方法和字段,同时通过重写__init__方法和Meta类,实现了对表单的个性化定制和扩展。