Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
...
like image 699
MHB Avatar asked Sep 14 '25 16:09

MHB


1 Answers

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 %}
like image 61
GTBebbo Avatar answered Sep 16 '25 06:09

GTBebbo