1

我正在尝试在模板中显示下拉菜单,但出现以下错误

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)
4

2 回答 2

3

与 Python 中的其他任何内容一样,您需要正确引用该对象。在这种情况下,Select 是通过表单模块提供的,就像 ChoiceField 一样。

selection = forms.ChoiceField(choices=MY_CHOICES, widget=forms.Select)
于 2014-06-19T12:15:03.283 回答
1

Python找不到Select,我看到你已经导入了django.forms,所以试试widget=forms.Select

于 2014-06-19T12:14:26.630 回答