Django1.5 forloop.counter未在模板

时间:2017-02-09 22:48:09

标签: html for-loop django-templates counter

我似乎无法让Djangos“forloop.counter”增加。我试图在网上关注教程无济于事。

首先,我使用 result_get_list 中存在的行填充模板。然后,我可以更改几个值并使用POST提交。现在,输出表中的每一行都是带有值的项目。我的控制器需要能够区分每一行。为此,我想附加一个id。这是您在下面看到的“ _1 ”(第二个代码块)。但不是在第一行附加“ _1 ”,而在第二行附加“ _2 ”, forloop .counter 无法按预期工作,并且每行都附加了“ _1 ”。

模板上的代码如下:

<form method="post">
{% csrf_token %}

    <div id="div_element">
    <table style=" float: left;" width="445">
    <tr>
        <th width="110"><center>Name</center></th>
        <th width="110"><center>Version</center></th>
        <th width="110"><center>Timestamp</center></th>
        <th width="110"><center>Action</center></th>
    </tr>
    {% if result_get_list %}
        {% for result_row in result_get_list %}
            {% for result_item in result_row %}
            <tr>
                <td width="110"><center><input type="text" readonly name="name_{{ forloop.counter }}" value={{result_item.name}}></center></td>
                <td width="110"><center><input type="text" readonly name="version_{{ forloop.counter }}" value={{result_item.version}}></center></td>
                <td width="110"><center><input type="text" name="date_{{ forloop.counter }}" value={{result_item.date}}></center></td>
                <td width="110"><center><select name="selected_post_action_{{ forloop.counter }}"><option>none</option><option>delete</option><option>update</option></select></center></td>
            </tr>
            {% endfor %}
       {% endfor %}
    {% endif %}
    </table>
    </div>

    <div id="div_nextline_section">
        <input type='submit' value='Update!'/>
    </div>

</form>

我的模板输出如下(查看html源代码)

<tr>
    <td width="110"><center><input type="text" readonly name="Name_1" value=ABCDEF></center></td>
    <td width="110"><center><input type="text" readonly name="Version_1" value=1></center></td>
    <td width="110"><center><input type="text" name="theDate_1" value=0></center></td>
    <td width="110"><center><select name="selected_post_action_1"><option>none</option><option>delete</option><option>expire</option><option>update</option></select></center></td>
</tr>
<tr>
    <td width="110"><center><input type="text" readonly name="Name_1" value=DEFGHI></center></td>
    <td width="110"><center><input type="text" readonly name="Version_1" value=1></center></td>
    <td width="110"><center><input type="text" name="theDate_1" value=0></center></td>
    <td width="110"><center><select name="selected_post_action_1"><option>none</option><option>delete</option><option>expire</option><option>update</option></select></center></td>
</tr>
<tr>
    <td width="110"><center><input type="text" readonly name="Name_1" value=GHIJKL></center></td>
    <td width="110"><center><input type="text" readonly name="Version_1" value=1></center></td>
    <td width="110"><center><input type="text" name="theDate_1" value=0></center></td>
    <td width="110"><center><select name="selected_post_action_1"><option>none</option><option>delete</option><option>expire</option><option>update</option></select></center></td>
</tr>

使用fiddler并捕获输出,这是POST到控制器的当前结果(注意“_1”)

name_1=ABCDEF&ersion_1=1&date_1=0&selected_post_action_1=delete&
name_1=DEFGHI&version_1=1&date_1=0&selected_post_action_1=expire&
name_1=GHIJKL&version_1=1&date_1=0&selected_post_action_1=update

我想要的输出是(注意每行递增)

name_1=ABCDEF&ersion_1=1&date_1=0&selected_post_action_1=delete&
name_2=DEFGHI&version_2=1&date_2=0&selected_post_action_2=expire&
name_3=GHIJKL&version_3=1&date_3=0&selected_post_action_3=update
非常感谢帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

想通了。我有一个嵌套的forloop。还有另一个参数用于引用父forloop: {{forloop.parentloop.counter}}

{% for result_row in result_get_list %}
    {% for result_item in result_row %}
    <tr>
        <td width="110"><center><input type="text" readonly name="name_{{forloop.parentloop.counter}}" value={{result_item.name}}></center></td>
        <td width="110"><center><input type="text" readonly name="version_{{forloop.parentloop.counter}}" value={{result_item.version}}></center></td>
        <td width="110"><center><input type="text" name="date_{{forloop.parentloop.counter}}" value={{result_item.date}}></center></td>
        <td width="110"><center><select name="selected_post_action_{{forloop.parentloop.counter}}"><option>none</option><option>delete</option><option>update</option></select></center></td>
    </tr>
    {% endfor %}
{% endfor %}