将属性添加到< input>标签为django ModelForm字段 [英] Adding an attribute to the <input> tag for a django ModelForm field
问题描述
但是,我希望我的html < input ...>
标签有一个额外的属性,即我想包括一个占位符
属性,如下所示:
< input placeholder ={{field.label}}... />
将这个属性添加到我的html中最简单的方法是什么?据我所知,我需要实现自己的过滤器来输出该字段,但是当我想做的所有事情都是一个人离开,但添加一个附加属性时,这似乎是过分的。
请参阅文档
类AuthorForm(ModelForm):
class Meta:
model = Author
widgets = {
'name':TextInput(attrs = {'placeholder':'name'}),
}
您可以随时创建自己的从TextInput派生的小部件,并包含占位符属性,并使用小部件字典只需将字段映射到新的小部件,而不必为每个字段指定占位符属性。
I have a django model that I'm displaying as a form using a ModelForm. The defaults work very well for me for the most part.
However, I would like my html <input ...>
tags to have one additional attribute, namely I would like to include a placeholder
attribute, like the following:
<input placeholder="{{field.label}}" ... />
What is the easiest way to add this attribute to my html? As far as I can tell it appears I need to implement my own filter to output the field, but this seems like overkill when all i want to do is leave everything alone but add one additional attribute.
See the documentation
class AuthorForm(ModelForm):
class Meta:
model = Author
widgets = {
'name': TextInput(attrs={'placeholder': 'name'}),
}
You could always create your own widget that derives from TextInput and includes the placeholder attribute, and use the widgets dictionary to simply map fields to your new widget without specifying the placeholder attribute for every field.
这篇关于将属性添加到< input>标签为django ModelForm字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!