dimanche 5 avril 2015

Django - Form - Fields : Modify the "for" attribute in

I have been stuck on this problem for hours and my research did not help. I'll make it quick:


I am building a form page and create it field-by field using



{{ myForm.myField }}


More precisely, 'myField' is a MultipleChoiceField. I am trying to tweak it so I can use it with a CSS template. Here its definition :



class myForm(models.ModelForm):
myField = forms.ChoiceField(choices=MY_CHOICES,
widget=RadioSelect(attrs={'data-toggle': 'radio',
'id': 'radio'}),
label='')


The goal here is to have a "for" attribute in "label" being "radio".


It is almost the case ! Here is the HTML result:



<div class="form-group">
<label for="radio_0">
<input class="" data-toggle="radio" (...) type="radio" value="1" />
</label>
<label for="radio_1">
<input class="" data-toggle="radio" (...) type="radio" value="2" />
</label>
<label for="radio_2">
<input class="" data-toggle="radio" (...) type="radio" value="3" />
</label>
</div>


As you can see, the "for" attribute is becoming "radio_0", "radio_1" and so forth but not simply "radio" which bothers me...


Would you know a way to solve this?


Thank you for your consideration.


Aucun commentaire:

Enregistrer un commentaire