简体   繁体   English

django模板中的切片和属性

[英]Slice and attribute in django template

I am tried to create a slice in django template and get an attribute. 我试图在django模板中创建一个切片并获取一个属性。 How can i do in django template something like this: 我怎么能在django模板中做这样的事情:

python code: python代码:

somelist[1].name

please help 请帮忙

If you are accessing a single element of a list, you don't need the slice filter, just use the dot notation. 如果要访问列表的单个元素,则不需要slice过滤器,只需使用点表示法。

{{ somelist.1.name }} 

See the docs for more info. 有关详细信息,请参阅文档

You can use the built-in tag slice with a combination of with : 您可以将内置标记slice与以下组合with

{% with somelist|slice:"1" as item %}
    {{ item.name }}
{% endwith %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM