Cha*_*pta 3 python jinja2 flask
我有一个列表长度为10的列表[a] [b].我想从列表[0] [b]打印到列表[10] [b]并在jinja2模板中使用它.
{% for i in test %}
<p> {{test[i][0]}} </p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
抛出错误:
UndefinedError: list object has no element
Run Code Online (Sandbox Code Playgroud)
当你迭代它时,你实际上从列表中获取元素,而不是索引值:
{% for row in test %}
{# Note that we subscript `row` directly,
(rather than attempting to index `test` with `row`) #}
<p>{{ row[0] }}</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如果你想确保始终拥有前10个:
{% for test in tests[0:10] %}
<p> {{ test[1] }} </p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12820 次 |
| 最近记录: |