python - 在 Django 模板中的元组列表的第一个元素中访问元组的第三项

标签 python django list django-templates tuples

<分区>

我的问题是: 我有一个元组列表作为要在 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/

相关文章:

Python如何动态改变变量的值

django - 在 GeoDjango 的自定义 View 中渲染 GeoQuerySet 的空间数据

android - 动态更新 PhoneGap 应用程序的 dojox EdgeToEdge 列表

java - Android 获取 IndexOutOfBundException

hibernate - 使用 Hibernate 映射双向列表

python - scikit-image - 缺少 DLL

python - 在 Python 中连接列表和字符串

python - 使用 matplotlib plot_surface 绘制地形数据

html - 当文本变长时,它会改变我的形式,也很难在正确的位置安装 div

Python/Django EmailMultiAlternatives 发送多封电子邮件