<分区>
我的问题是: 我有一个元组列表作为要在 django 模板文件中访问的变量。
我只想访问列表中第一个元组的第三项。
例如: 如果列表是:
[(1,2,3,4),(5,6,7,8),(9,10,11,12)]
我只想要“3”。
我试过类似的方法:
{{lista | first . 3}}
和
{{(lista | first).3}}
但这些都不起作用。
<分区>
我的问题是: 我有一个元组列表作为要在 django 模板文件中访问的变量。
我只想访问列表中第一个元组的第三项。
例如: 如果列表是:
[(1,2,3,4),(5,6,7,8),(9,10,11,12)]
我只想要“3”。
我试过类似的方法:
{{lista | first . 3}}
和
{{(lista | first).3}}
但这些都不起作用。
最佳答案
在模板中,您应该能够使用点符号和 django will unpack it for you :
{{ lista.0.2 }}
Dots have a special meaning in template rendering. A dot in a variable name signifies a lookup. Specifically, when the template system encounters a dot in a variable name, it tries the following lookups, in this order:
- Dictionary lookup. Example:
foo["bar"]
- Attribute lookup. Example:
foo.bar
- List-index lookup. Example:
foo[bar]
关于python - 在 Django 模板中的元组列表的第一个元素中访问元组的第三项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34982536/