2017-08-07 207 views
0

我應該如何在template.html中命名我的選擇標籤,以便在每次接收到POST消息時能夠將它們分別關聯到views.py中?如何在html中動態命名標籤以便稍後與它們關聯?

<div style="display: inline-block;"> 
    {% for type in range(form.typesCount) %} 
    <div> 
     <select name="???"> 
      {% for format in formats %} 
      <option value="{{format}}"> {{ format }}</option> 
      {% endfor %} 
     </select> 
    </div> 
    {% endfor %} 
</div> 

回答

1

您可以使用loop.index,這是返回神社for環內對當前迭代指數的整數。例如:

{% for type in range(form.typesCount) %} 

    <select name="my-select-{{loop.index}}"> 
     ... 
    </select> 

{% endfor %} 
+0

有趣的是,爲什麼loop.index從1開始? – sramij

+1

'loop.index0'從零開始。請參閱上面的文檔鏈接。 – darksky

相關問題