Ticket #4228: radioselect_renderer.patch
File radioselect_renderer.patch, 2.6 KB (added by , 17 years ago) |
---|
-
django/newforms/widgets.py
20 20 'HiddenInput', 'MultipleHiddenInput', 21 21 'FileInput', 'Textarea', 'CheckboxInput', 22 22 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', 23 'RadioFieldRenderer', 23 24 'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget', 24 25 ) 25 26 … … 258 259 return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % w for w in self]) 259 260 260 261 class RadioSelect(Select): 262 field_renderer = RadioFieldRenderer 263 264 def __init__(self, *args, **kwargs): 265 field_renderer = kwargs.pop('field_renderer', None) 266 if field_renderer: 267 self.field_renderer = field_renderer 268 super(RadioSelect, self).__init__(*args, **kwargs) 269 261 270 def render(self, name, value, attrs=None, choices=()): 262 271 "Returns a RadioFieldRenderer instance rather than a Unicode string." 263 272 if value is None: value = '' 264 273 str_value = smart_unicode(value) # Normalize to string. 265 274 final_attrs = self.build_attrs(attrs) 266 return RadioFieldRenderer(name, str_value, final_attrs, list(chain(self.choices, choices)))275 return self.field_renderer(name, str_value, final_attrs, list(chain(self.choices, choices))) 267 276 268 277 def id_for_label(self, id_): 269 278 # RadioSelect is represented by multiple <input type="radio"> fields, -
tests/regressiontests/forms/tests.py
688 688 <li><label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li> 689 689 </ul> 690 690 691 # Override the field_renderer to change the output 692 >>> class MyRenderer(RadioFieldRenderer): 693 ... def __unicode__(self): 694 ... return u',\n'.join([u'%s' % w for w in self]) 695 >>> w = RadioSelect(field_renderer=MyRenderer) 696 >>> print w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 697 <label><input type="radio" name="beatle" value="J" /> John</label>, 698 <label><input type="radio" name="beatle" value="P" /> Paul</label>, 699 <label><input checked="checked" type="radio" name="beatle" value="G" /> George</label>, 700 <label><input type="radio" name="beatle" value="R" /> Ringo</label> 701 691 702 # CheckboxSelectMultiple Widget ############################################### 692 703 693 704 >>> w = CheckboxSelectMultiple()