Hellnar
Hellnar

Reputation: 64793

Django template question

How can I achieve this using the Django template system:

Say I have 2 variable passed to the template system:

days=[1,2,3,4,5]
items=[ {name:"apple,day:3},{name:"orange,day:5} ]

I want to have such output as a table:

        1   2    3    4    5
apple   n   n    y    n    n
orange  n   n    n    n    y

As you can notice, giving "n" to non matching ones and "y" to matching.

Upvotes: 1

Views: 203

Answers (3)

Daniel Roseman
Daniel Roseman

Reputation: 599530

Here's what Ignacio meant. That said, I probably agree with Daniel that you should do this in the view.

<table>
{% for item in items %}
  <tr>
    <td>{% item.name %}</td>
    {% for dday in days %}
    <td>
      {% ifequal dday item.day %}y{% else %}n{% endifequal %}
    </td>
    {% endfor %}
  </tr>
{% endfor %}
</table>

I've called the days loop variable 'dday' to make it clear that the lookup item.day here is actually getting item['day'].

Upvotes: 6

Daniel Vassallo
Daniel Vassallo

Reputation: 344291

Why don't you define this logic in the django view, and then simply pass arrays of Ys and Ns to the template?

Upvotes: 6

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798566

Two loops. The outer loop is through items, the inner through days. Test if outer[day] is equal to inner, and output y if so and n if not.

Upvotes: 2

Related Questions