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.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5065 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
39aa40d108
commit
56b8914eb9
|
@ -260,8 +260,8 @@ class RadioSelect(Select):
|
||||||
"Returns a RadioFieldRenderer instance rather than a Unicode string."
|
"Returns a RadioFieldRenderer instance rather than a Unicode string."
|
||||||
if value is None: value = ''
|
if value is None: value = ''
|
||||||
str_value = smart_unicode(value) # Normalize to string.
|
str_value = smart_unicode(value) # Normalize to string.
|
||||||
attrs = attrs or {}
|
final_attrs = self.build_attrs(attrs)
|
||||||
return RadioFieldRenderer(name, str_value, attrs, list(chain(self.choices, choices)))
|
return RadioFieldRenderer(name, str_value, final_attrs, list(chain(self.choices, choices)))
|
||||||
|
|
||||||
def id_for_label(self, id_):
|
def id_for_label(self, id_):
|
||||||
# RadioSelect is represented by multiple <input type="radio"> fields,
|
# RadioSelect is represented by multiple <input type="radio"> fields,
|
||||||
|
@ -327,14 +327,25 @@ class MultiWidget(Widget):
|
||||||
if not isinstance(value, list):
|
if not isinstance(value, list):
|
||||||
value = self.decompress(value)
|
value = self.decompress(value)
|
||||||
output = []
|
output = []
|
||||||
|
final_attrs = self.build_attrs(attrs)
|
||||||
|
id_ = final_attrs.get('id', None)
|
||||||
for i, widget in enumerate(self.widgets):
|
for i, widget in enumerate(self.widgets):
|
||||||
try:
|
try:
|
||||||
widget_value = value[i]
|
widget_value = value[i]
|
||||||
except KeyError:
|
except IndexError:
|
||||||
widget_value = None
|
widget_value = None
|
||||||
output.append(widget.render(name + '_%s' % i, widget_value, attrs))
|
if id_:
|
||||||
|
final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
|
||||||
|
output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
|
||||||
return self.format_output(output)
|
return self.format_output(output)
|
||||||
|
|
||||||
|
def id_for_label(self, id_):
|
||||||
|
# See the comment for RadioSelect.id_for_label()
|
||||||
|
if id_:
|
||||||
|
id_ += '_0'
|
||||||
|
return id_
|
||||||
|
id_for_label = classmethod(id_for_label)
|
||||||
|
|
||||||
def value_from_datadict(self, data, name):
|
def value_from_datadict(self, data, name):
|
||||||
return [data.get(name + '_%s' % i) for i in range(len(self.widgets))]
|
return [data.get(name + '_%s' % i) for i in range(len(self.widgets))]
|
||||||
|
|
||||||
|
|
|
@ -658,10 +658,31 @@ Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
IndexError: list index out of range
|
IndexError: list index out of range
|
||||||
|
|
||||||
|
# Unicode choices are correctly rendered as HTML
|
||||||
>>> w = RadioSelect()
|
>>> w = RadioSelect()
|
||||||
>>> unicode(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]))
|
>>> unicode(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]))
|
||||||
u'<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>'
|
u'<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>'
|
||||||
|
|
||||||
|
# Attributes provided at instantiation are passed to the constituent inputs
|
||||||
|
>>> w = RadioSelect(attrs={'id':'foo'})
|
||||||
|
>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
|
||||||
|
<ul>
|
||||||
|
<li><label><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
|
||||||
|
<li><label><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
|
||||||
|
<li><label><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
|
||||||
|
<li><label><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
# Attributes provided at render-time are passed to the constituent inputs
|
||||||
|
>>> w = RadioSelect()
|
||||||
|
>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'})
|
||||||
|
<ul>
|
||||||
|
<li><label><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
|
||||||
|
<li><label><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
|
||||||
|
<li><label><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>
|
||||||
|
<li><label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
# CheckboxSelectMultiple Widget ###############################################
|
# CheckboxSelectMultiple Widget ###############################################
|
||||||
|
|
||||||
>>> w = CheckboxSelectMultiple()
|
>>> w = CheckboxSelectMultiple()
|
||||||
|
@ -783,6 +804,11 @@ u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li
|
||||||
u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />'
|
u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />'
|
||||||
>>> w.render('name', 'john__lennon')
|
>>> w.render('name', 'john__lennon')
|
||||||
u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />'
|
u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />'
|
||||||
|
>>> w.render('name', 'john__lennon', attrs={'id':'foo'})
|
||||||
|
u'<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" />'
|
||||||
|
>>> w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})), attrs={'id': 'bar'})
|
||||||
|
>>> w.render('name', ['john', 'lennon'])
|
||||||
|
u'<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" />'
|
||||||
|
|
||||||
# SplitDateTimeWidget #########################################################
|
# SplitDateTimeWidget #########################################################
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue