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. Form field with fixed value by roam 1 week, 3 days ago
  2. New Snippet! by Antoliny0919 2 weeks, 2 days ago
  3. Add Toggle Switch Widget to Django Forms by OgliariNatan 3 months ago
  4. get_object_or_none by azwdevops 6 months, 3 weeks ago
  5. Mask sensitive data from logger by agusmakmun 8 months, 3 weeks ago

Comments

ragq1991 (on February 6, 2023):

I found your question and answer on StackOverFlow and ended up here accordingly. THANK YOU VERY MUCH! (translated with Google Translate). Для русскоязычных постскриптум: регистрация здесь просто пи*дец.

#

Please login first before commenting.