Ticket #8103: 8103.2.diff

File 8103.2.diff, 3.3 KB (added by Chris Beaven, 14 years ago)
  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index cb12586..fc80732 100644
    a b class CheckboxInput(Widget):  
    495495        return bool(initial) != bool(data)
    496496
    497497class Select(Widget):
     498    allow_multiple_selected = False
     499
    498500    def __init__(self, attrs=None, choices=()):
    499501        super(Select, self).__init__(attrs)
    500502        # choices can be any iterable, but we may need to render this widget
    class Select(Widget):  
    514516
    515517    def render_option(self, selected_choices, option_value, option_label):
    516518        option_value = force_unicode(option_value)
    517         selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
     519        if option_value in selected_choices:
     520            selected_html = u' selected="selected"'
     521            if not self.allow_multiple_selected:
     522                # Only allow for a single selection.
     523                selected_choices.remove(option_value)
     524        else:
     525            selected_html = ''
    518526        return u'<option value="%s"%s>%s</option>' % (
    519527            escape(option_value), selected_html,
    520528            conditional_escape(force_unicode(option_label)))
    class NullBooleanSelect(Select):  
    567575        return initial != data
    568576
    569577class SelectMultiple(Select):
     578    allow_multiple_selected = True
     579
    570580    def render(self, name, value, attrs=None, choices=()):
    571581        if value is None: value = []
    572582        final_attrs = self.build_attrs(attrs, name=name)
  • tests/regressiontests/forms/widgets.py

    diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
    index 10483a7..785b302 100644
    a b over multiple times without getting consumed:  
    463463<option value="4">4</option>
    464464</select>
    465465
     466Only one option can be selected:
     467>>> print w.render('choices', 0, choices=(('0', 'extra'),))
     468<select name="choices">
     469<option value="0" selected="selected">0</option>
     470<option value="1">1</option>
     471<option value="2">2</option>
     472<option value="3">3</option>
     473<option value="4">4</option>
     474<option value="0">extra</option>
     475</select>
     476
     477Ensure that it still selects the first element next time round:
     478>>> print w.render('choices', 0, choices=(('0', 'extra'),))
     479<select name="choices">
     480<option value="0" selected="selected">0</option>
     481<option value="1">1</option>
     482<option value="2">2</option>
     483<option value="3">3</option>
     484<option value="4">4</option>
     485<option value="0">extra</option>
     486</select>
     487
    466488Choices can be nested one level in order to create HTML optgroups:
    467489>>> w.choices=(('outer1', 'Outer 1'), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))))
    468490>>> print w.render('nestchoice', None)
    True  
    674696>>> w._has_changed([1, 2], [u'1', u'3'])
    675697True
    676698
     699Multiple options (with the same value) can be selected:
     700>>> print w.render('choices', [1], choices=(('1', 'extra'),))
     701<select multiple="multiple" name="choices">
     702<option value="1" selected="selected">1</option>
     703<option value="2">2</option>
     704<option value="3">3</option>
     705<option value="1" selected="selected">extra</option>
     706</select>
     707
    677708# Choices can be nested one level in order to create HTML optgroups:
    678709>>> w.choices = (('outer1', 'Outer 1'), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))))
    679710>>> print w.render('nestchoice', None)
Back to Top