我在表单中有一个选择字段,现在我需要在此字段中迭代选项.
{{ form.myselect }} 给我这个:
<select name="myselect" id="id_myselect">
<option value="" selected="selected">---------</option>
<option value="2">Item 1</option>
<option value="3">Item 2</option>
...
</select>
Run Code Online (Sandbox Code Playgroud)
现在我需要为选项添加一些属性,因此我需要的是:
<select name="myselect" id="id_myselect">
{% for x in form.myselect %}
<option value="{{ x.id }}">{{ x.name }}</option>
{% endfor %}
</select>
Run Code Online (Sandbox Code Playgroud)
但是有一个错误:
Caught TypeError while rendering: 'BoundField' object is not iterable
Run Code Online (Sandbox Code Playgroud)
我试过form.myselect.all,form.myselect.option_set但它没有给出任何东西
PLANNING_CHOICES = (
('0',u'Every morning'),
('1',u'Every night'),
('2',u'Never'),
)
planning = forms.ChoiceField(
required=True,
choices = PLANNING_CHOICES,
)
Run Code Online (Sandbox Code Playgroud)
有一个名为planning的表单字段,我需要在选项中添加title属性,最后呈现为:
<select>
<option value="0" title="bla1">Every morning</option>
<option value="1" title="bla2">Every night</option>
<option value="2" title="bla3">Never</option>
</select>
Run Code Online (Sandbox Code Playgroud)
如何实现?