From df4d3c0e0ffd61006c56c0ef04d7f31a9fc1f61c Mon Sep 17 00:00:00 2001 From: Todd Dembrey <todd.dembrey@torchbox.com> Date: Wed, 31 Jan 2018 10:25:47 +0000 Subject: [PATCH] Update the template to output tidier html --- .../templates/funds/widgets/nested_with_label.html | 14 ++++++++++++++ opentech/apply/funds/widgets.py | 4 +++- .../django/forms/widgets/multiwidget.html | 1 - 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 opentech/apply/funds/templates/funds/widgets/nested_with_label.html delete mode 100644 opentech/templates/django/forms/widgets/multiwidget.html diff --git a/opentech/apply/funds/templates/funds/widgets/nested_with_label.html b/opentech/apply/funds/templates/funds/widgets/nested_with_label.html new file mode 100644 index 000000000..c1c7d0d48 --- /dev/null +++ b/opentech/apply/funds/templates/funds/widgets/nested_with_label.html @@ -0,0 +1,14 @@ +<div {% include "django/forms/widgets/attrs.html" %}>{% spaceless %} + {% for widget in widget.subwidgets %} + {% if not widget.subwidgets %} + <div class="form__item"> + <label for="{{ widget.attrs.id }}">{{ widget.attrs.name.title }}</label> + {% endif %} + + {% include widget.template_name %} + + {% if not widget.subwidgets %} + </div> + {% endif %} + {% endfor %} +{% endspaceless %}</div> diff --git a/opentech/apply/funds/widgets.py b/opentech/apply/funds/widgets.py index 153726080..78b3ec452 100644 --- a/opentech/apply/funds/widgets.py +++ b/opentech/apply/funds/widgets.py @@ -6,7 +6,7 @@ from django_countries import countries class KeepOwnAttrsWidget(forms.Widget): def get_context(self, name, value, attrs): - name = self.attrs.pop('name') or name + name = self.attrs.get('name') or name attrs.update(self.attrs) return super().get_context(name, value, attrs) @@ -22,6 +22,8 @@ class KeepAttrsTextInput(KeepOwnAttrsWidget, forms.TextInput): class NestedMultiWidget(forms.MultiWidget): + template_name = 'funds/widgets/nested_with_label.html' + def __init__(self, *args, **kwargs): widgets = [ widget(attrs={'name': field}) for field, widget in self.components.items() diff --git a/opentech/templates/django/forms/widgets/multiwidget.html b/opentech/templates/django/forms/widgets/multiwidget.html deleted file mode 100644 index cf6424162..000000000 --- a/opentech/templates/django/forms/widgets/multiwidget.html +++ /dev/null @@ -1 +0,0 @@ -<div {% include "django/forms/widgets/attrs.html" %}>{% spaceless %}{% for widget in widget.subwidgets %}{% include widget.template_name %}{% endfor %}{% endspaceless %}</div> -- GitLab