Django

Code

Ticket #3442: example.py

File example.py, 0.7 kB (added by tbarta@gmail.com, 1 year ago)

example usage of GroupedSelect

Line 
1 import os
2 os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
3
4 from django.newforms import forms, fields, widgets
5 from django.newforms.extras import widgets as extrawidgets
6
7 choicedict = dict(
8     a='one',
9     b='two',
10     c='three',
11     yellow='banana',
12     green='pear',
13     red='apple')
14
15 groups = (
16     ('letters', ('a', 'b', 'c')),
17     ('colors', ('yellow', 'red', 'green')))
18
19 choices = choicedict.items()
20
21 class MyForm(forms.Form):
22     linear = fields.ChoiceField(choices=choices, widget=widgets.Select)
23     grouped = fields.ChoiceField(choices=choices, widget=extrawidgets.GroupedSelect(groups=groups))
24
25 f = MyForm(dict(linear='yellow', grouped='yellow'))
26 print f.as_ul()