Django

Code

Changeset 5065

Show
Ignore:
Timestamp:
04/24/07 07:53:29 (1 year ago)
Author:
russellm
Message:

Fixed #3870, Refs #3787 -- Fixed handling of widget attributes on RadioSelect? and MultiWidget?. In particular, handling of the id attribute has been fixed. Thanks to Gary Wilson and Max Derkachev.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/widgets.py

    r4961 r5065  
    261261        if value is None: value = '' 
    262262        str_value = smart_unicode(value) # Normalize to string. 
    263         attrs = attrs or {} 
    264         return RadioFieldRenderer(name, str_value, attrs, list(chain(self.choices, choices))) 
     263        final_attrs = self.build_attrs(attrs) 
     264        return RadioFieldRenderer(name, str_value, final_attrs, list(chain(self.choices, choices))) 
    265265 
    266266    def id_for_label(self, id_): 
     
    328328            value = self.decompress(value) 
    329329        output = [] 
     330        final_attrs = self.build_attrs(attrs) 
     331        id_ = final_attrs.get('id', None) 
    330332        for i, widget in enumerate(self.widgets): 
    331333            try: 
    332334                widget_value = value[i] 
    333             except KeyError: 
     335            except IndexError: 
    334336                widget_value = None 
    335             output.append(widget.render(name + '_%s' % i, widget_value, attrs)) 
     337            if id_: 
     338                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) 
     339            output.append(widget.render(name + '_%s' % i, widget_value, final_attrs)) 
    336340        return self.format_output(output) 
     341 
     342    def id_for_label(self, id_): 
     343        # See the comment for RadioSelect.id_for_label() 
     344        if id_: 
     345            id_ += '_0' 
     346        return id_ 
     347    id_for_label = classmethod(id_for_label) 
    337348 
    338349    def value_from_datadict(self, data, name): 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4961 r5065  
    659659IndexError: list index out of range 
    660660 
     661# Unicode choices are correctly rendered as HTML 
    661662>>> w = RadioSelect() 
    662663>>> unicode(w.render('email', 'ŠĐĆŜćşšđ', choices=[('ŠĐĆŜćşšđ', 'ŠĐabcĆŜćşšđ'), ('ćşšđ', 'abcćşšđ')])) 
    663664u'<ul>\n<li><label><input checked="checked" type="radio" name="email" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="radio" name="email" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>' 
     665 
     666# Attributes provided at instantiation are passed to the constituent inputs 
     667>>> w = RadioSelect(attrs={'id':'foo'}) 
     668>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 
     669<ul> 
     670<li><label><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li> 
     671<li><label><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li> 
     672<li><label><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li> 
     673<li><label><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li> 
     674</ul> 
     675 
     676# Attributes provided at render-time are passed to the constituent inputs 
     677>>> w = RadioSelect() 
     678>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'}) 
     679<ul> 
     680<li><label><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li> 
     681<li><label><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li> 
     682<li><label><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li> 
     683<li><label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li> 
     684</ul> 
    664685 
    665686# CheckboxSelectMultiple Widget ############################################### 
     
    784805>>> w.render('name', 'john__lennon') 
    785806u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />' 
     807>>> w.render('name', 'john__lennon', attrs={'id':'foo'}) 
     808u'<input id="foo_0" type="text" class="big" value="john" name="name_0" /><br /><input id="foo_1" type="text" class="small" value="lennon" name="name_1" />' 
     809>>> w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})), attrs={'id': 'bar'}) 
     810>>> w.render('name', ['john', 'lennon']) 
     811u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />' 
    786812 
    787813# SplitDateTimeWidget #########################################################