I am using Django form for inputs. However, I would like to custom it a little bit. For example the following Django code will be translated into:
#Django code
aerial_size_dist = forms.ChoiceField(initial='Very Fine to Fine')
#Translated HTML
<tr><th><label for="id_aerial_size_dist">Aerial size dist:</label></th><td><select name="aerial_size_dist" id="id_aerial_size_dist"></select></td></tr>
My question is that how to add a label property such as "style" from working from the Django side? Can widget change Django form label property?
<tr><th><label for="id_aerial_size_dist" style="display:none;">Aerial size dist:</label></th><td><select name="aerial_size_dist" id="id_aerial_size_dist"></select></td></tr>
Similar problem is answered here: Get the ID of a field widget in a formset . Although it doesn't allow you to customize the ID directly, it uses default (autogenerated) ID
(Short answer) use this in template:
EDIT:
<label for="{{ form.my_field.id_for_label }}">{{ form.my_field.label }}</label>
old one: (this was not working in some cases - see comments)
<label for="{{ form.my_field.auto_id }}">{{ form.my_field.label }}</label>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With