if I have a variable in the context of unknown length, for example;
list=[{\'key\':\'A\'},{\'key\':\'B\'},{\'key\':\'C\'}]
How can I get the last ob
without with, will be:
{% set last = list|last %}
{{ last.key }}
Use this piece of code in your template:
{{ list|slice:":-1".items.0.0 }}
You can mark the last forloop by the following tags
{% if forloop.last %} ... {% endif %}
and add you special desire inside the tags.
Use the last template tag:
{{ value|last }}
If value is the list
['a', 'b', 'c', 'd']
, the output will be the string"d"
.
Thanks everyone for you help, it lead me to the realisation that I can use the with tag.
{% with list|last as last %}
{{ last.key }}
{% endwith %}
In my case I find this solution: {{object_list.last.id}}
very useful on expression like this: {% url 'my-url' object_list.first.id object_list.last.id %}