From 924930d273aebde36ac085fcc0d5c610774d85b8 Mon Sep 17 00:00:00 2001 From: Todd Dembrey Date: Tue, 6 Feb 2018 12:19:20 +0000 Subject: [PATCH] Update the display value to be nicer for non js --- .../widgets/nested_with_label.html | 2 +- addressfield/widgets.py | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/addressfield/templates/addressfield/widgets/nested_with_label.html b/addressfield/templates/addressfield/widgets/nested_with_label.html index d3e88f4c..82afcdc8 100644 --- a/addressfield/templates/addressfield/widgets/nested_with_label.html +++ b/addressfield/templates/addressfield/widgets/nested_with_label.html @@ -2,7 +2,7 @@ {% for widget in widget.subwidgets %} {% if not widget.subwidgets %}
- + {% endif %} {% include widget.template_name %} diff --git a/addressfield/widgets.py b/addressfield/widgets.py index 7c8928f4..9a8f4aa7 100644 --- a/addressfield/widgets.py +++ b/addressfield/widgets.py @@ -20,18 +20,27 @@ class KeepAttrsTextInput(KeepOwnAttrsWidget, forms.TextInput): pass +def classify(field): + return field.replace('_', '') + + +def display(field): + return field.replace('_', ' ').title() + + class NestedMultiWidget(KeepOwnAttrsWidget, forms.MultiWidget): template_name = 'addressfield/widgets/nested_with_label.html' def __init__(self, *args, **kwargs): widgets = [ - widget(attrs={'class': field, 'required': False}) for field, widget in self.components.items() + widget(attrs={'class': classify(field), 'required': False, 'display': display(field)}) + for field, widget in self.components.items() ] super().__init__(widgets, *args, **kwargs) @property def field_names(self): - return list(self.components.keys()) + return [classify(field) for field in self.components.keys()] def decompress(self, value): if value: @@ -58,9 +67,9 @@ def value_from_datadict(self, data, files, name): class LocalityWidget(NestedMultiWidget): components = { - 'localityname': KeepAttrsTextInput, - 'administrativearea': KeepAttrsTextInput, - 'postalcode': KeepAttrsTextInput, + 'locality_name': KeepAttrsTextInput, + 'administrative_area': KeepAttrsTextInput, + 'postal_code': KeepAttrsTextInput, }