[Django]-Access tuple in django template

69đź‘Ť

âś…

Assuming your view code is:

t=[]
t.extend([('a',1),('b',2),('c',3)])

(and not as stated in the OP)

{{ t.0.0 }} is like t[0][0] in Python code. This should give you “a”, because t.0 is the first element of the list t, which itself is a tuple, and then another .0 is the tuple’s first element.

{{ t.0.1 }} will be 1, and so on.

But in your question you are creating a tuple and trying to access it as if it is a dict.

That’s the problem.

6đź‘Ť

You can convert your tuple to dict via dict() function:

mydict = dict(t)

And then in template you can access items by key like here:

{{ mydict|get_item:item.NAME }}

Leave a comment