Html 在for循环django模板的每个循环中生成唯一id
我需要在每个循环中生成唯一的id,而不是选定的城市```Html 在for循环django模板的每个循环中生成唯一id,html,django,django-templates,frontend,Html,Django,Django Templates,Frontend,我需要在每个循环中生成唯一的id,而不是选定的城市``` {% 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
{% 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 %}
您可以使用{{forloop.counter}}。它以数字形式给出循环迭代
看
您可以使用{{forloop.counter}}。它以数字形式给出循环迭代
看
这回答了你的问题吗?不,首先我需要它在django模板的前端。第二,我需要类似city-select-I的东西,但我不知道如何生成该ID并在名称和ID部分使用它@这能回答你的问题吗?看看第二个答案。我如何在id部分使用生成的文本@IvanStarostinid=city select-{{variable}}这是否回答了您的问题?不,首先我需要它在django模板的前端。第二,我需要类似city-select-I的东西,但我不知道如何生成该ID并在名称和ID部分使用它@这能回答你的问题吗?看看第二个答案。我如何在id部分使用生成的文本@IvanStarostinid=城市选择-{{variable}
city-1
city-2
city-3
...
{% for field in form %}
<!-- your html -->
city-{{ forloop.counter }}
{% endfor %}