How do I use Django's form framework for select options?

后端 未结 4 1088
旧巷少年郎
旧巷少年郎 2021-02-12 15:15

http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Select

Here, it says I can do SELECT widgets. But how do I do that? It doesn\'t show any example on

4条回答
  •  我寻月下人不归
    2021-02-12 15:22

    Django 2.0

    Options = [
            ('1', 'Hello'),
            ('2', 'World'),
          ]
    category = forms.ChoiceField(label='Category', widget=forms.Select, choices=sample)
    

    BTW tuple also works as same as list.

    Options = (
            ('1', 'Hello'),
            ('2', 'World'),
         )
    category = forms.ChoiceField(label='Category', widget=forms.Select, choices=sample)
    

提交回复
热议问题