[Django]-Get list values in a Django Template

7๐Ÿ‘

โœ…

{{ headers.1.url }}

From http://docs.djangoproject.com/en/dev/topics/templates/#variables:

Technically, when the template system encounters a dot, it tries the following lookups, in this order:
    * Dictionary lookup
    * Attribute lookup
    * Method call
    * List-index lookup

So, instead of {{ headers|slice:โ€1โ€ณ }} you can do {{ headers.1 }}. And then to access the url key, you just append it: {{ headers.1.url }}.

HTH.

๐Ÿ‘คdijxtra

0๐Ÿ‘

Iโ€™m not sure I understand your question but to get the values form a dict in the template you can use the methods items or values.

If you use {{dict.items}} you get a list of tuples (key, value) and you can get the value simply using tuple.1.

If you use {{dict.values}} you just get a list of the values of the dictionary.

๐Ÿ‘คFacundo Casco

-1๐Ÿ‘

I think you want:

{% with headers|slice:"1" as data %}
<a href="{{ data.url }}"{{ data.class_attr|safe }}>{{ data.text }}</a>
{% endwith %}
๐Ÿ‘คSmileyChris

Leave a comment