'
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('
', id_) if id_ else '
'
- output = [start_tag]
+ output = []
for i, choice in enumerate(self.choices):
choice_value, choice_label = choice
if isinstance(choice_label, (tuple, list)):
@@ -677,14 +678,16 @@ class ChoiceFieldRenderer(object):
attrs=attrs_plus,
choices=choice_label)
sub_ul_renderer.choice_input_class = self.choice_input_class
- output.append(format_html('
""")
+
+ # Unicode choices are correctly rendered as HTML
+ w = RadioSelect()
+ self.assertHTMLEqual(six.text_type(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])), '
\n\n\n
')
+
+ # 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ćžšđ')])), '
\n\n\n
')
-
- # 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 = '