Login

Template filter to get a list element by its index

Author:
abidibo
Posted:
May 3, 2012
Language:
Python
Version:
1.4
Score:
1 (after 1 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

  1. "Magic Link" Management Command by webology 2 months, 3 weeks ago
  2. Closest ORM models to a latitude/longitude point by simonw 2 months, 3 weeks ago
  3. Log the time taken to execute each DB query by kennyx46 2 months, 3 weeks ago
  4. django database snippet by ItsRLuo 2 months, 3 weeks ago
  5. Serialize a model instance by chriswedgwood 3 months, 3 weeks ago

Comments

Please login first before commenting.