I have a list say list[a][b] of length 10. I want to print from list[0][b] to list[10][b] and use it in jinja2 template.
{% for i in test %}
<p> {{test[i][0]}} </p>
{% endfor %}
throws error:
UndefinedError: list object has no element
当您迭代它时,您实际上将元素从列表中取出,而不是索引值:
{% for row in test %}
{# Note that we subscript `row` directly,
(rather than attempting to index `test` with `row`) #}
<p>{{ row[0] }}</p>
{% endfor %}
如果您想确保始终拥有前 10 个:
{% for test in tests[0:10] %}
<p> {{ test[1] }} </p>
{% endfor %}