Django:如何将任意 html 属性添加到表单上的输入字段? [英] Django: How do I add arbitrary html attributes to input fields on a form?
本文介绍了Django:如何将任意 html 属性添加到表单上的输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用模板呈现的输入字段,如下所示:
{{ form.city }}
呈现为:
<input id="id_city" type="text" name="city" maxlength="100"/>
现在假设我想向呈现的输入元素添加一个 autocomplete="off"
属性,我该怎么做?还是 onclick="xyz()"
或 class="my-special-css-class"
?
解决方案
city = forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}))
I have an input field that is rendered with a template like so:
<div class="field">
{{ form.city }}
</div>
Which is rendered as:
<div class="field">
<input id="id_city" type="text" name="city" maxlength="100" />
</div>
Now suppose I want to add an autocomplete="off"
attribute to the input element that is rendered, how would I do that? Or onclick="xyz()"
or class="my-special-css-class"
?
解决方案
city = forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}))
这篇关于Django:如何将任意 html 属性添加到表单上的输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文