Reputation: 2212
I am trying to build a form containing a CheckboxSelectMultiple widget. When using the build-in rendering like so
{{ f.activities }}
I get the following result
<ul id="id_reviewitem_set-0-activities">
<li>
<label for="id_reviewitem_set-0-activities_0">
<input type="checkbox" name="reviewitem_set-0-activities" value="1" id="id_reviewitem_set-0-activities_0" />
Test
</label>
</li>
...
An ID is generated for the field as a whole (as seen in the ul tag) and for each individual option (as seen in the input tag). This is the result I would expect.
However, if I do the whole thing manually like so
<ul id="{{ f.activities.id_for_label }}">
{% for option in f.activities %}
<li>
{{ option }}
</li>
{% endfor %}
</ul>
The ID for the activities field is not returned, so I get
<ul id="">
<li>
<label for="id_reviewitem_set-0-activities_0">
<input type="checkbox" name="reviewitem_set-0-activities" value="1" id="id_reviewitem_set-0-activities_0" />
Test
</label>
</li>
...
It would be useful for me to have the id as I am using it for some js stuff. Is it possible to access the id of the field (i.e. "id_reviewitem_set-0-activities") or do I need to create my own in the template?
Upvotes: 0
Views: 152
Reputation: 1003
as per the docs:
The outer <ul> container receives the id attribute of the widget, if defined, or BoundField.auto_id otherwise.
thus you can get the id you're looking for like this:
<ul id="{{ f.activities.auto_id }}">
{% for option in f.activities %}
<li>
{{ option }}
</li>
{% endfor %}
</ul>
Upvotes: 1