1

I am trying to put {{field.id}} in the the HTML input attributes but it doesn't work. What is the correct way to do this?

<input type="text" class="form-control" id="{{field.id}}">

I also tried:

<input type="text" class="form-control" id={{field.id}}>

Heres my full code:

{% for field in wizard.form %}


   <div class="row">
    <label class="col-xs-4" for={{field.id_for_label}}>{{ field.label }}</label>
      <div class="col-xs-2">   


       <div class="form-group">
            <input type="text" class="form-control" id="{{field.id}}">
            {{field.id}}




        </div>


      </div>
    </div>
  {% endfor %}

2 Answers 2

2

You can get the generated id of a form field like so:

{{ field.auto_id }}

This is a duplicate of https://stackoverflow.com/a/3765016/1637351

Edit: Though I don't understand why you're trying to put the id value as the name attribute. That will result in the rendered html being

<input name="id_field_name" />
Sign up to request clarification or add additional context in comments.

Comments

1

I would be taken care of automatically if using the field template tag.

Instead, I'd try a different approach than you're using, using django_widget_tweaks to programmatically add attributes.

For example, in your template...

{% for field in wizard.form %}
   <div class="row">
    <label class="col-xs-4" for={{field.auto_id}}>{{ field.label }}</label>
      <div class="col-xs-2">   
       <div class="form-group">
            {{field|attr:'class:form-control}}
        </div>
      </div>
    </div>
  {% endfor %}

This will automatically generate the input for each field with class='form-control' as part of the tag. The id/name will automatically be taken care of by rendering field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.