Setting ID for Radio buttons in Django

后端 未结 2 1068
天涯浪人
天涯浪人 2021-01-21 03:06

It is great that Django 1.4 allows fine graining of radio select

{% for radio in form.important_client reversed%}
      {{radio.tag}}
2条回答
  •  时光说笑
    2021-01-21 03:38

    This is one way to do that, it might not be the best but it works. In your form you can set the id for each of the choices like this:

    from django import forms
    class MyForm(forms.Form):
        CHOICES = (('1','Available'),('2','Not Available'))
        input   = forms.ChoiceField(widget=RadioSelect(attrs={'id' : 'myId'},choices=CHOICES)
    

    Then on your template:

    {% for radio in form.input %}
        {{ radio }}
    {% endfor %}
    

    And your HTML will look like this:

     Available
     Not Available
    

    I hope this works!

提交回复
热议问题