简体   繁体   中英

Slice and attribute in django template

I am tried to create a slice in django template and get an attribute. How can i do in django template something like this:

python code:

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.

{{ somelist.1.name }} 

See the docs for more info.

You can use the built-in tag slice with a combination of with :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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