Login

Template filter to get a list element by its index

Author:
abidibo
Posted:
May 3, 2012
Language:
Python
Version:
1.4
Tags:
filter template-filter list get_at_index
Score:
0 (after 0 ratings)

Gets the list element at the given index

Example

{% for item1 in list1 %}
  <p>List1 element: {{ item1 }}</p>
  <p>List2 element: {{ list2|get_at_index:forloop.counter0 }}
{% endfor %}
1
2
3
4
5
6
7
from django import template

register = template.Library()

@register.filter
def get_at_index(list, index):
    return list[index]

More like this

Comments

Please login first before commenting.