http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Select
在这里,它说我可以做SELECT小部件。但是我该怎么做? 它没有显示如何在python中编写该字段的任何示例。
<select>
<option>option 1</option>
<option>option 2</option>
</select>
答案 0 :(得分:13)
CHOICES= (
('ME', '1'),
('YOU', '2'),
('WE', '3'),
)
select = forms.CharField(widget=forms.Select(choices=CHOICES))
答案 1 :(得分:9)
class MyForm(forms.Form):
CHOICES = (('Option 1', 'Option 1'),('Option 2', 'Option 2'),)
field = forms.ChoiceField(choices=CHOICES)
print MyForm().as_p()
# out: <p><label for="id_field">Field:</label> <select name="field" id="id_field">\n<option value="Option 1">Option 1</option>\n<option value="Option 2">Option 2</option>\n</select></p>
答案 2 :(得分:8)
CHOICES= (
('1','ME'),
('2','YOU'),
('3','WE'),
)
select = forms.ChoiceField(widget=forms.Select, choices=CHOICES)
CHOICES内的元素对应于($ option_value,$ option_text)。
答案 3 :(得分:0)
Django 2.0
Options = [
('1', 'Hello'),
('2', 'World'),
]
category = forms.ChoiceField(label='Category', widget=forms.Select, choices=sample)
BTW元组也与列表相同。
Options = (
('1', 'Hello'),
('2', 'World'),
)
category = forms.ChoiceField(label='Category', widget=forms.Select, choices=sample)