MHB
MHB

Reputation: 695

generate unique id in each loop of for loop django template

I need to generate unique id in each loop instead of ````city-selected```

{% for form in formset.forms %}
    <tr>
        {% for field in form %}
            <td class="input_td{% if field.errors %} error_td{% endif %}">
                <select name="city-select" id="city-select"></select>
            </td>
        {% endfor %}
        <td class="delete_formset_td"></td>
    </tr>
{% endfor %}

How can I generate it here?

I need some thing like this for ids:

output:

city-1
city-2
city-3
...

Upvotes: 4

Views: 2117

Answers (1)

GTBebbo
GTBebbo

Reputation: 1218

You can use {{ forloop.counter }}. It gives you the loop iteration as a number.

See here.

{% for field in form %}

    <!-- your html -->

    city-{{ forloop.counter }}

{% endfor %}

Upvotes: 7

Related Questions