JD Yang
JD Yang

Reputation: 371

How to index elements of a tuple in Django template using a variable?

I have week_infos tuple and pass it to template

{{ week_infos.4.0 }}

This works. However the following does not:

{% for j in 7|get_range %}
    <td row={{j}} col={{i|add:6}}>
        {{  week_infos.j }}

How can I access a specific item item in a tuple?

Upvotes: 0

Views: 411

Answers (1)

Soviut
Soviut

Reputation: 91555

You seem to be using the range in order to number your rows, however, you can do the same thing using a for loop's counter if you loop over the week_infos tuple:

{% for week_info in week_infos %}
    <td row="{{ forloop.counter0 }}" col="{{ i|add:6 }}">
        {{ week_info }}

(Also, you should always wrap HTML attributes in quotes since they can contain spaces)

Upvotes: 3

Related Questions