Django Templates First element of a List

橙三吉。 提交于 2019-12-03 04:38:00

You can use the {% with %} templatetag for this sort of thing.

{% with v.docs|first as first_doc %}{{ first_doc.id }}{% endwith %}
Abdul Majeed

You can try this:

{{ v.docs.0 }}

Like arr.0

You can get elements by index (0, 1, 2, etc.).

I don't know if this is helpful..

What you want is the first value of an iterable (v.docs) and you are iterating over another encapsulating iterable (lists).

For the count, I would do the same, but for the first element.. I'd iterate over the v.docs individually and retrieve the first value via an inner loop.

{% for doc in v.docs %}
    {% if v.docs | first %}  
    <li>doc</li>
    {% endif %}
{% endfor %}

Note: the first filter is applied to v.docs , not doc. Yeah. It involves another loop :(

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!