2017-02-09 54 views
0

我似乎無法讓Djangos「forloop.counter」增加。我試圖在網絡上按照教程無濟於事。Django1.5 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> 

使用招並捕捉輸出這是當前結果它被髮送到控制器(注意「_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 

的幫助感激,謝謝!

+0

我要補充,這是使用Python 2.7的Django 1.5 – New2Python

回答

0

想通了。我有一個嵌套的forloop。還有另外一個參數引用父for循環:{{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 %}