From dd9a23d5cfb44cb6f150610c267d0de1d963b849 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 4 Jul 2014 13:53:01 +0200 Subject: [PATCH] Fixed #22950 -- Eased markup customization for choice field rendering Thanks Patrick Robertson for the report. --- django/forms/widgets.py | 17 +++-- tests/forms_tests/tests/test_widgets.py | 99 +++++++++++++++---------- 2 files changed, 68 insertions(+), 48 deletions(-) diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 4abc135f26..ee8ab5c129 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -643,6 +643,8 @@ class ChoiceFieldRenderer(object): """ choice_input_class = None + outer_html = '{content}' + inner_html = '
  • {choice_value}{sub_widgets}
  • ' def __init__(self, name, value, attrs, choices): self.name = name @@ -664,8 +666,7 @@ class ChoiceFieldRenderer(object): item in the list will get an id of `$id_$i`). """ id_ = self.attrs.get('id', None) - start_tag = format_html('""") + # Choices are escaped correctly + w = RadioSelect() + self.assertHTMLEqual(w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))), """""") + + # Unicode choices are correctly rendered as HTML + w = RadioSelect() + self.assertHTMLEqual(six.text_type(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])), '') + + # Attributes provided at instantiation are passed to the constituent inputs + w = RadioSelect(attrs={'id': 'foo'}) + self.assertHTMLEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), """""") + + # Attributes provided at render-time are passed to the constituent inputs + w = RadioSelect() + self.assertHTMLEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id': 'bar'}), """""") + + def test_radiofieldrenderer(self): # RadioSelect uses a RadioFieldRenderer to render the individual radio inputs. # You can manipulate that object directly to customize the way the RadioSelect # is rendered. @@ -648,6 +678,18 @@ beatle J P Paul False beatle J G George False beatle J R Ringo False""") + # A RadioFieldRenderer object also allows index access to individual RadioChoiceInput + w = RadioSelect() + r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) + self.assertHTMLEqual(str(r[1]), '') + self.assertHTMLEqual(str(r[0]), '') + self.assertTrue(r[0].is_checked()) + self.assertFalse(r[1].is_checked()) + self.assertEqual((r[1].name, r[1].value, r[1].choice_value, r[1].choice_label), ('beatle', 'J', 'P', 'Paul')) + + with self.assertRaises(IndexError): + r[10] + # You can create your own custom renderers for RadioSelect to use. class MyRenderer(RadioFieldRenderer): def render(self): @@ -667,46 +709,21 @@ beatle J R Ringo False""")
    """) - # A RadioFieldRenderer object also allows index access to individual RadioChoiceInput - w = RadioSelect() - r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) - self.assertHTMLEqual(str(r[1]), '') - self.assertHTMLEqual(str(r[0]), '') - self.assertTrue(r[0].is_checked()) - self.assertFalse(r[1].is_checked()) - self.assertEqual((r[1].name, r[1].value, r[1].choice_value, r[1].choice_label), ('beatle', 'J', 'P', 'Paul')) - - with self.assertRaises(IndexError): - r[10] - - # Choices are escaped correctly - w = RadioSelect() - self.assertHTMLEqual(w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))), """""") - - # Unicode choices are correctly rendered as HTML - w = RadioSelect() - self.assertHTMLEqual(six.text_type(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])), '') - - # Attributes provided at instantiation are passed to the constituent inputs - w = RadioSelect(attrs={'id': 'foo'}) - self.assertHTMLEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), """""") - - # Attributes provided at render-time are passed to the constituent inputs - w = RadioSelect() - self.assertHTMLEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id': 'bar'}), """""") + # You can customize rendering with outer_html/inner_html renderer variables (#22950) + class MyRenderer(RadioFieldRenderer): + outer_html = str('{content}') # str is just to test some Python 2 issue with bytestrings + inner_html = '

    {choice_value}{sub_widgets}

    ' + w = RadioSelect(renderer=MyRenderer) + output = w.render('beatle', 'J', + choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), + attrs={'id': 'bar'}) + self.assertIsInstance(output, SafeData) + self.assertHTMLEqual(output, """
    +

    +

    +

    +

    +
    """) def test_nested_choices(self): # Choices can be nested for radio buttons: