Django 模板:如何访问列表中第一项的属性 [英] Django Templating: how to access properties of the first item in a list

查看:15
本文介绍了Django 模板:如何访问列表中第一项的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单.我有一个要传递给 Django 模板的 Python 列表.

Pretty simple. I have a Python list that I am passing to a Django template.

我可以使用

{{ thelist|first }}

但是,我也想访问该项目的属性...理想情况下,您会认为它看起来像这样:

However, I also want to access a property of that item... ideally you'd think it would look like this:

{{ thelist|first.propertyName }}

可惜,它没有.

是否有任何模板解决方案,或者我只是发现自己传递了一个额外的模板变量...

Is there any template solution to this, or am I just going to find myself passing an extra template variable...

推荐答案

您可以通过索引号访问列表中的任何项目.在模板中,这与任何其他属性查找的工作方式相同:

You can access any item in a list via its index number. In a template this works the same as any other property lookup:

{{ thelist.0.propertyName }}

这篇关于Django 模板:如何访问列表中第一项的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆