diff --git a/addressfield/templates/addressfield/widgets/nested_with_label.html b/addressfield/templates/addressfield/widgets/nested_with_label.html index d3e88f4c955edfbec21e59c4d543a331418870ac..82afcdc8acd46ecb4a3126dd64d196d6a4eb23d7 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 %} <div class="form__item"> - <label for="{{ widget.attrs.id }}">{{ widget.attrs.class.title }}</label> + <label for="{{ widget.attrs.id }}">{{ widget.attrs.display }}</label> {% endif %} {% include widget.template_name %} diff --git a/addressfield/widgets.py b/addressfield/widgets.py index 7c8928f482d79de063f337115c116a32b0ca89a0..9a8f4aa7e359623248253a9cc85731edf9ff8654 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 @@ class NestedMultiWidget(KeepOwnAttrsWidget, forms.MultiWidget): class LocalityWidget(NestedMultiWidget): components = { - 'localityname': KeepAttrsTextInput, - 'administrativearea': KeepAttrsTextInput, - 'postalcode': KeepAttrsTextInput, + 'locality_name': KeepAttrsTextInput, + 'administrative_area': KeepAttrsTextInput, + 'postal_code': KeepAttrsTextInput, }