Django 如何迭代模板中SelectField的选项?
表单中有一个select字段,现在我需要迭代该字段中的选项Django 如何迭代模板中SelectField的选项?,django,django-forms,django-templates,Django,Django Forms,Django Templates,表单中有一个select字段,现在我需要迭代该字段中的选项 {{form.myselect}}告诉我: <select name="myselect" id="id_myselect"> <option value="" selected="selected">---------</option> <option value="2">Item 1</option> <option value="3">
{{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>
我尝试了
form.myselect.all
,form.myselect.option\u集
,但它没有给出任何结果我今天一直在努力解决这个问题,并找到了解决方案。是的,您可以直接在模板中迭代选择标记的选项。以下是如何在模板中执行此操作:
<select id="id_customer" name="customer">
{% for x, y in form.fields.customer.choices %}
<option value="{{ x }}"{% if form.fields.customer.value == x %} selected{% endif %}>{{ y }}</option>
{% endfor %}
</select>
希望这有助于使用它:
<select name="myselect" class="i-can-add-my-own-attrs-now" id="id_myselect">
{% for id, name in form.myselect.field.choices %}
<option value="{{ id }}">{{ name }}</option>
{% endfor %}
</select>
使用django小部件调整也将为您设置默认的“selected=”selected“,这非常好 使用模板中的单选按钮
<table>
{% for x,y in form.fields.Customer.choices %}
<tr>
<td><input id="id_Customer_{{x}}" {% if form.fields.Customer.value == x %}checked="checked"{% endif %} name="Customer" type="radio" value="{{x}}" /></td>
<td>{{ y }}</td>
</tr>
{% endfor %}
</table>
{%x,y在form.fields.Customer.choices%}
{{y}
{%endfor%}
我这样做:
<select id="id_construction_type" name="construction_type" class="form-control input-md">
{% for value, key in form_urban.fields.construction_type.choices %}
<option value="{{ value }}"{% if form_urban.initial.construction_type == value %} selected {% endif %}>
{{ key }}
</option>
{% endfor %}
</select>
{%为值,输入表单_urban.fields.construction_type.choices%}
{{key}}
{%endfor%}
这是一个更干净的解决方案,您可以使用自定义小部件设置属性。这样,您就不必手动渲染字段:
class CustomSelectWidget(forms.Select):
def create_option(self, name, value, *args, **kwargs):
option = super().create_option(name, value, *args, **kwargs)
if value:
instance = self.choices.queryset.get(pk=value) # get instance
option['attrs']['custom_attr'] = instance.field_name # set option attribute
return option
class SomeForm(forms.ModelForm):
some_field = forms.ModelChoiceField(
queryset=SomeModel.objects.all(),
widget=CustomSelectWidget
)
那么您想要的是所有的
,没有
,也没有空白(----
)选项?还是我错过了什么。。。你想在这里具体实现什么?不,我想在选项中添加一些属性,正因为如此,我需要在`{%forx in form.myselect%}`循环中使用它。我的建议是修改小部件并在代码中使用它:谢谢James。我希望有某种方法可以迭代模板中的选项。我也遇到了同样的问题,除了添加:{%forc in form.fields.customer.queryset%}{{c.name | capfirst}}{%endfor%}
设置所选选项对我来说不起作用。我不得不使用form.fields.Customer.value
而不是form.initial.Customer
,但这只会将类添加到一个本身,而不是每个选项字段。是否可以使用此插件重复选择?
<table>
{% for x,y in form.fields.Customer.choices %}
<tr>
<td><input id="id_Customer_{{x}}" {% if form.fields.Customer.value == x %}checked="checked"{% endif %} name="Customer" type="radio" value="{{x}}" /></td>
<td>{{ y }}</td>
</tr>
{% endfor %}
</table>
<select id="id_construction_type" name="construction_type" class="form-control input-md">
{% for value, key in form_urban.fields.construction_type.choices %}
<option value="{{ value }}"{% if form_urban.initial.construction_type == value %} selected {% endif %}>
{{ key }}
</option>
{% endfor %}
</select>
class CustomSelectWidget(forms.Select):
def create_option(self, name, value, *args, **kwargs):
option = super().create_option(name, value, *args, **kwargs)
if value:
instance = self.choices.queryset.get(pk=value) # get instance
option['attrs']['custom_attr'] = instance.field_name # set option attribute
return option
class SomeForm(forms.ModelForm):
some_field = forms.ModelChoiceField(
queryset=SomeModel.objects.all(),
widget=CustomSelectWidget
)