[Answer]-Django Forms BoundField – Output when iterating over ChoiceField has no id tag

1πŸ‘

βœ…

I don’t know why it works like that. My guess would be that those id’s are created by an iterator created by the forms api. A single country is not aware of the iterator (it could appear in the template without appearing within a for loop), therefor there’s no way of creating the id.

To answer your other question – Yes you could build the inputs yourself to include the id’s.

something like –

{% for country in form.country %}
    <li>
        <img src="{{ MEDIA_URL }}country_icons/country_{{ country.value }}.png">
        <label for="id_country_{{ forloop.counter0 }}">
            <input id="id_country_{{ forloop.counter0 }}" type="radio" value="{{ country.value }}" name={{ country }}>
        </label>
        {{ country.name }}
    </li>
{% endfor %}

Have a look at the docs on template for loops

πŸ‘€Aidan Ewen

Leave a comment