diff options
| author | Claude Paroz <claude@2xlibre.net> | 2014-07-04 13:53:01 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2014-07-05 13:36:49 +0200 |
| commit | dd9a23d5cfb44cb6f150610c267d0de1d963b849 (patch) | |
| tree | d9a71a15c10e4c72cc1c567b38f848431c16e94b /django/forms/widgets.py | |
| parent | 9209049211acbe1a53c3dc409dd5fe26edf21634 (diff) | |
Fixed #22950 -- Eased markup customization for choice field rendering
Thanks Patrick Robertson for the report.
Diffstat (limited to 'django/forms/widgets.py')
| -rw-r--r-- | django/forms/widgets.py | 17 |
1 files changed, 10 insertions, 7 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 = '<ul{id_attr}>{content}</ul>' + inner_html = '<li>{choice_value}{sub_widgets}</li>' 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('<ul id="{0}">', id_) if id_ else '<ul>' - 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('<li>{0}{1}</li>', choice_value, - sub_ul_renderer.render())) + output.append(format_html(self.inner_html, choice_value=choice_value, + sub_widgets=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}</li>', force_text(w))) - output.append('</ul>') - return mark_safe('\n'.join(output)) + output.append(format_html(self.inner_html, + choice_value=force_text(w), sub_widgets='')) + return format_html(self.outer_html, + id_attr=format_html(' id="{0}"', id_) if id_ else '', + content=mark_safe('\n'.join(output))) class RadioFieldRenderer(ChoiceFieldRenderer): |
