increment a variable in django templates

后端 未结 3 1780
不知归路
不知归路 2021-02-05 22:45

All,

How Can we increment a value like the following in django templates,

  {{ flag =0 }}

  {% for op in options %}
   {{op.choices}}

        
3条回答
  •  忘掉有多难
    2021-02-05 23:08

    I don't think it's intended you should alter data in your templates. For in your specific case, you could instead use the forloop.counter variable.

    For example:

    {% for op in options %}
      {{op.choices}}
    {% endfor %}
    

    Also note that I added that number to the id attributes of the tag. Otherwise you'll have multiple inputs with the same id.

    EDIT: I didn't note that it was a radio input. You could of course have the same name for each .

提交回复
热议问题