From cdbd5751d3c9579bac7054b45edae11de27171ff Mon Sep 17 00:00:00 2001 From: Gary Wilson Jr Date: Wed, 1 Aug 2007 05:41:32 +0000 Subject: [PATCH] Fixed #4228 -- Removed hardcoding of `RadioFieldRenderer` in the `RadioSelect` Widget so that the display of `RadioSelect`s can be more easily customized. `BoundField.__unicode__` also no longer special cases `RadioSelect` since `RadioSelect.render()` now returns a string like every other Widget. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5782 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/forms.py | 21 +++++++++--------- django/newforms/widgets.py | 33 +++++++++++++++++++++++----- tests/regressiontests/forms/tests.py | 18 ++++++++++++--- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 50e6f42108..5da85a69c4 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -232,16 +232,8 @@ class BoundField(StrAndUnicode): self.help_text = field.help_text or '' def __unicode__(self): - "Renders this field as an HTML widget." - # Use the 'widget' attribute on the field to determine which type - # of HTML widget to use. - value = self.as_widget(self.field.widget) - if not isinstance(value, basestring): - # Some Widget render() methods -- notably RadioSelect -- return a - # "special" object rather than a string. Call __unicode__() on that - # object to get its rendered value. - value = unicode(value) - return value + """Renders this field as an HTML widget.""" + return self.as_widget() def _errors(self): """ @@ -251,7 +243,14 @@ class BoundField(StrAndUnicode): return self.form.errors.get(self.name, ErrorList()) errors = property(_errors) - def as_widget(self, widget, attrs=None): + def as_widget(self, widget=None, attrs=None): + """ + Renders the field by rendering the passed widget, adding any HTML + attributes passed as attrs. If no widget is specified, then the + field's default widget will be used. + """ + if not widget: + widget = self.field.widget attrs = attrs or {} auto_id = self.auto_id if auto_id and 'id' not in attrs and 'id' not in widget.attrs: diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index b90e6df9cd..e9b9b55470 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -216,7 +216,11 @@ class SelectMultiple(Widget): return data.get(name, None) class RadioInput(StrAndUnicode): - "An object used by RadioFieldRenderer that represents a single ." + """ + An object used by RadioFieldRenderer that represents a single + . + """ + def __init__(self, name, value, attrs, choice, index): self.name, self.value = name, value self.attrs = attrs @@ -239,7 +243,10 @@ class RadioInput(StrAndUnicode): return u'' % flatatt(final_attrs) class RadioFieldRenderer(StrAndUnicode): - "An object used by RadioSelect to enable customization of radio widgets." + """ + An object used by RadioSelect to enable customization of radio widgets. + """ + def __init__(self, name, value, attrs, choices): self.name, self.value, self.attrs = name, value, attrs self.choices = choices @@ -253,16 +260,30 @@ class RadioFieldRenderer(StrAndUnicode): return RadioInput(self.name, self.value, self.attrs.copy(), choice, idx) def __unicode__(self): - "Outputs a