我试图在我的模板中显示下拉菜单,但出现以下错误
File "forms.py" in DropdownSelectionForm
101. selection = forms.ChoiceField(choices=MY_CHOICES, widget = Select)
Exception Type: NameError at /
Exception Value: name 'Select' is not defined
这是我试图显示表单的模板
<form action="/doclistings/" method="post" >{% csrf_token %}
<select class="form-control" id="s1" NAME="selection">
<option><b>Find a Doctor...</b></option>
{% for value, text in form.selection.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
这里是forms.py
MY_CHOICES = (
('Dermatologist', 'Dermatologist'),
('Dentist', 'Dentist'),
('Opthalmologist', 'Opthalmologist'),
)
class DropdownSelectionForm(forms.Form):
selection = forms.ChoiceField(choices=MY_CHOICES, widget = Select)
最佳答案
与 Python 中的其他任何内容一样,您需要正确引用对象。在这种情况下,Select 通过表单模块提供,与 ChoiceField 完全一样。
selection = forms.ChoiceField(choices=MY_CHOICES, widget=forms.Select)
关于python - Django 1.6 名称选择未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24306298/