Ticket #11843: 11843.2.diff
File 11843.2.diff, 3.3 KB (added by , 15 years ago) |
---|
-
django/forms/widgets.py
247 247 def render(self, name, value, attrs=None, choices=()): 248 248 if value is None: value = [] 249 249 final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) 250 return mark_safe(u'\n'.join([(u'<input%s />' % 251 flatatt(dict(value=force_unicode(v), **final_attrs))) 252 for v in value])) 250 id_attr = final_attrs.get('id') 251 inputs = [] 252 for i, v in enumerate(value): 253 input_attrs = dict(value=force_unicode(v), **final_attrs) 254 if id_attr: 255 # An ID attribute was given. Add a numeric index as a suffix 256 # so that the inputs don't all have the same ID attribute. 257 input_attrs['id'] = '%s_%s' % (id_attr, i) 258 inputs.append(u'<input%s />' % flatatt(input_attrs)) 259 return mark_safe(u'\n'.join(inputs)) 253 260 254 261 def value_from_datadict(self, data, files, name): 255 262 if isinstance(data, (MultiValueDict, MergeDict)): -
tests/regressiontests/forms/widgets.py
181 181 >>> w.render('email', ['foo@example.com'], attrs={'class': 'special'}) 182 182 u'<input type="hidden" class="special" value="foo@example.com" name="email" />' 183 183 184 Each input gets a separate ID. 185 >>> w = MultipleHiddenInput() 186 >>> w.render('letters', list('abc'), attrs={'id': 'hideme'}) 187 u'<input type="hidden" name="letters" value="a" id="hideme_0" />\n<input type="hidden" name="letters" value="b" id="hideme_1" />\n<input type="hidden" name="letters" value="c" id="hideme_2" />' 188 184 189 # FileInput Widget ############################################################ 185 190 186 191 FileInput widgets don't ever show the value, because the old value is of no use … … 1014 1019 >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) 1015 1020 u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>\n<li><label><input type="checkbox" name="nums" value="2" /> 2</label></li>\n<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>\n<li><label><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>' 1016 1021 1022 # Each input gets a separate ID 1023 >>> print CheckboxSelectMultiple().render('letters', list('ac'), choices=zip(list('abc'), list('ABC')), attrs={'id': 'abc'}) 1024 <ul> 1025 <li><label for="abc_0"><input checked="checked" type="checkbox" name="letters" value="a" id="abc_0" /> A</label></li> 1026 <li><label for="abc_1"><input type="checkbox" name="letters" value="b" id="abc_1" /> B</label></li> 1027 <li><label for="abc_2"><input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" /> C</label></li> 1028 </ul> 1029 1017 1030 # MultiWidget ################################################################# 1018 1031 1019 1032 >>> class MyMultiWidget(MultiWidget):