[Answer]-Django โ€“ Getting form field by string var

1๐Ÿ‘

My option is not pretty, but it must work:

{% with fieldname=field.html_name|add:'_min' %}
  <tr class="collapse" id="{{ field.html_name }}">
         <td>{{ field.label }}</td>
         {% if fieldname == "aant_inw_min" %}
         <td>{% render_field form.aant_inw_min class+="form-control" %}</td>
         {% elif fieldname == "aant_inw_max" %}
         <td>{% render_field form.aant_inw_max class+="form-control" %}</td>
         {% elif fieldname == "aant_inw_priority" %}
         <td>{% render_field form.aant_inw_priority class+="form-control" %}</td>
         {% endif %}
  </tr>
{% endwith %}

or just create a custom templatetag.

๐Ÿ‘คMikeVelazco

Leave a comment