6

My problem is: I have a list of tuples as a variable to be accessed in a django template file.

I just want to access the third item of the first tuple in the list.

For example: If the list is:

[(1,2,3,4),(5,6,7,8),(9,10,11,12)] 

I just want the "3".

I've tried something like:

{{lista | first . 3}}

and

{{(lista | first).3}}

But neither of these work.

  • The fact that you're looking to extract the "third of the first" anything to me kind of indicates that you either need a class/model to represent data or something may be wrong logically – Sayse Jan 24 '16 at 23:29
  • It makes sense, but in this case the data came from a raw SQL query. – Roberto Assunção Filho Jan 26 '16 at 00:54

1 Answers1

7

Inside the template you should be able to use dot notation and django will unpack it for you:

{{ lista.0.2 }}

Dots have a special meaning in template rendering. A dot in a variable name signifies a lookup. Specifically, when the template system encounters a dot in a variable name, it tries the following lookups, in this order:

  • Dictionary lookup. Example: foo["bar"]
  • Attribute lookup. Example: foo.bar
  • List-index lookup. Example: foo[bar]