How can reference the last item in a list in a Django template? {{ list.-1.key }}
Use the last
template tag:
{{ value|last }}
If value is the list
['a', 'b', 'c', 'd']
, the output will be the string"d"
.
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 %}
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 %}