Ticket #4785: div-widgets.diff
File div-widgets.diff, 2.3 KB (added by , 17 years ago) |
---|
-
django/newforms/widgets.py
239 239 return u'<input%s />' % flatatt(final_attrs) 240 240 241 241 class RadioFieldRenderer(StrAndUnicode): 242 tags = { 'block' : u'<ul>\n%s\n</ul>', 'item': u'<li>%s</li>' } 242 243 "An object used by RadioSelect to enable customization of radio widgets." 243 244 def __init__(self, name, value, attrs, choices): 244 245 self.name, self.value, self.attrs = name, value, attrs … … 254 255 255 256 def __unicode__(self): 256 257 "Outputs a <ul> for this set of radio fields." 257 return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>'% force_unicode(w) for w in self])258 return self.tags['block'] % u'\n'.join([self.tags['item'] % force_unicode(w) for w in self]) 258 259 259 260 class RadioSelect(Select): 260 261 def render(self, name, value, attrs=None, choices=()): … … 275 276 id_for_label = classmethod(id_for_label) 276 277 277 278 class CheckboxSelectMultiple(SelectMultiple): 279 tags = { 'block' : u'<ul>\n%s\n</ul>', 'item' : u'<li><label>%(input)s %(label)s</label></li>' } 278 280 def render(self, name, value, attrs=None, choices=()): 279 281 if value is None: value = [] 280 282 has_id = attrs and 'id' in attrs 281 283 final_attrs = self.build_attrs(attrs, name=name) 282 output = [ u'<ul>']284 output = [] 283 285 str_values = set([force_unicode(v) for v in value]) # Normalize to strings. 284 286 for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): 285 287 # If an ID attribute was given, add a numeric index as a suffix, … … 289 291 cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) 290 292 option_value = force_unicode(option_value) 291 293 rendered_cb = cb.render(name, option_value) 292 output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(force_unicode(option_label)))) 293 output.append(u'</ul>') 294 return u'\n'.join(output) 294 output.append(self.tags['item'] % {'input': rendered_cb, 'label': escape(force_unicode(option_label))}) 295 return self.tags['block'] % u'\n'.join(output) 295 296 296 297 def id_for_label(self, id_): 297 298 # See the comment for RadioSelect.id_for_label()