Get the widget id of the field in the form set

I want to customize the layout of the forms in the form set (that is, I do not want to use .as_table () or .as_p (), etc.). I am trying to get the name of a form field to use in its label for, but I'm not sure how to do it. I hope that I will not need to create a new name / identifier for the field from scratch. Here is an example of what I'm working with right now:

{% for form in formset.forms %}
<!-- The field for the "path" form field -->
<label for="{{what do I put here?}}">{{form.fields.path.label}}:</label><input type="text" id="{{django creates this one; do I have to do my own with the for loop counter or something?}}" name="{{probably the same as id}}" />
{% endfor %}

Is there any type of "create an identifier for a form set field"?

+1
source share
2 answers

Most likely you want to.

for="{{ form.your_field.html_name }}"
+5
source

First you want to use the id of the form element, not the name.

I tried Django 1.3 Alpha-1 and worked:

{% for form in formset.forms %}
    <label for="{{ form.my_field.auto_id }}">{{ form.my_field.label }}</label>
    {{ form.my_field }}
{% endfor %}

Enjoy it!

+2
source

All Articles