Get list item dynamically in django templates

This is not possible directly because Django thinks that "x" is the key to lookup in mylist – instead of the value of x. So, when x = 5, Django tries to look up mylist["x"] instead of mylist[5].

Use the following filter as workaround:

@register.filter
def lookup(d, key):
    return d[key]

and use it like

{{ mylist|lookup:x }}

Leave a Comment