def render(self): """ Outputs a <ul> for this set of choice fields. If an id was given to the field, it is applied to the <ul> (each item in the list will get an id of `$id_$i`). """ id_ = self.attrs.get('id', None) start_tag = format_html('<ul id="{0}" class="multiple_cloumns double">', id_) if id_ else '<ul>' output = [start_tag] for i, choice in enumerate(self.choices): choice_value, choice_label = choice img = mark_safe('<img class="fac-background fac-%s"> </img>'%choice_value) if isinstance(choice_label, (tuple, list)): attrs_plus = self.attrs.copy() if id_: attrs_plus['id'] += '_{0}'.format(i) sub_ul_renderer = ChoiceFieldRenderer(name=self.name, value=self.value, attrs=attrs_plus, choices=choice_label) sub_ul_renderer.choice_input_class = self.choice_input_class output.append(format_html('<li>{0}{1}{2}</li>', force_text(img), choice_value, sub_ul_renderer.render())) else: w = self.choice_input_class(self.name, self.value, self.attrs.copy(), choice, i) output.append(format_html('<li>{0} {1}</li>', force_text(img), force_text(w))) output.append('</ul>') return mark_safe('\n'.join(output))
def test_choice_field_renderer_html_safe(self): renderer = ChoiceFieldRenderer('choices', 'CHOICE1', {}, [('CHOICE1', 'first_choice')]) renderer.choice_input_class = lambda *args: args self.assertTrue(hasattr(ChoiceFieldRenderer, '__html__')) self.assertEqual(force_text(renderer), renderer.__html__())
def test_choice_field_renderer_html_safe(self): renderer = ChoiceFieldRenderer("choices", "CHOICE1", {}, [("CHOICE1", "first_choice")]) renderer.choice_input_class = lambda *args: args self.assertTrue(hasattr(ChoiceFieldRenderer, "__html__")) self.assertEqual(force_text(renderer), renderer.__html__())