Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在for循环django模板的每个循环中生成唯一id_Html_Django_Django Templates_Frontend - Fatal编程技术网

Html 在for循环django模板的每个循环中生成唯一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

我需要在每个循环中生成唯一的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"></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 %}