Python for 循环内 jinja2 中的范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17691838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:56:08 来源:igfitidea点击:
range in jinja2 inside a for loop
提问by Chandan Gupta
I have a nested list. I need to iterate through a list and keep it in for loop as shown below.
我有一个嵌套列表。我需要遍历一个列表并将其保存在 for 循环中,如下所示。
{% for alpha in list %}
<div id="{{ loop.index }}">
<div class='sidebar-one'>
{% for beta in list[0][2:] %} #I want to iterate through list[0][2:] till list[n][2:]
<p> {{ beta[0][0] }} </p>
{% endfor %}
</div>
</div>
{% endfor %}
I tried range but no luck.
我尝试了范围但没有运气。
{% for n in range(1,n) %}
{% for line in check[{{n}}][2:] %}
{% endfor %}
it threw error:
它抛出错误:
TemplateSyntaxError: expected token ':', got '}'
采纳答案by Blender
It's just like Python:
这就像 Python:
{% for n in range(n) %}
{% for line in check[n][2:] %}
<p> {{ beta[0][0] }} </p>
{% endfor %}
{% endfor %}
回答by Dinidiniz
You can use the "length" property:
您可以使用“长度”属性:
{% for n in range(yourList| length) %}
<p class="someclass">{{n + 1}}.</p>
<a class="someclass2"
href="{{ url_for( 'yourFunction', Int = yourList[n].iterable)}}">
{{yourList[n].iterable}}</a><br>
{% endfor %}
Length is similar to len(yourlist) that we have in python.
长度类似于我们在 python 中的 len(yourlist) 。