我试图在我的模板中显示下拉菜单,但是我收到以下错误
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)
答案 0 :(得分:3)
与Python中的任何其他内容一样,您需要正确引用该对象。在这种情况下,Select通过表单模块提供,与ChoiceField完全相同。
selection = forms.ChoiceField(choices=MY_CHOICES, widget=forms.Select)
答案 1 :(得分:1)
Python无法找到Select
,我发现您已导入django.forms
,因此请尝试widget=forms.Select