Using a 2-dimensional array in a template

32 views
Skip to first unread message

Yves de Champlain

unread,
Jul 16, 2019, 8:47:38 PM7/16/19
to Django users
Hi

I'm pretty new to Django. I like it a lot but there seems to be no straightforward way to use a 2-dimensional array in a template, something that might look like this : 

{% for skill in skills %}
   
<tr>
       
<td>{{ skill.name }}</td>

   
{% for achievement in achievements %}

       
<td> {{ v_table[forloop.parentloop.counter0][forloop.counter0] }}</td>
   
{% endfor %}
   
</tr>
{% endfor %}

I'm wondering why there is no simple way of doing such an evident operation as building a table in Django ? Right now my solution is to build the html table in views.py as a string and pass that string to the template but that feels terribly wrong ... so what would be the right way to do that ?

Thanks a lot !

Daniel Roseman

unread,
Jul 18, 2019, 10:24:04 AM7/18/19
to Django users
You haven't really given enough context to answer the question. What are `skills` and `achievements`? How are they related, and how are either of them related to `v_table`? Normally in Django you would iterate directly over a relationship by following foreign keys - why can't you do that? Post your models and view.
--
DR.

Yves de Champlain

unread,
Jul 18, 2019, 10:56:59 AM7/18/19
to Django users
Indeed, it feels like I'm taking things the wrong way, maybe I coded too much PHP to be redeemable ...

My main problem might be coming from the fact that I need to parse the data to generate my view and can't simply pull the data from the DB.

Here are the models, template and view attached, as well as a screenshot of the result. 

Please note that as far as the logic of the app is concerned, "Competency" means the same as "Skill". I need to fix that dual terminology in the code. 

Thanks a lot for your answer.

yves
Skills-models.py
Skills.html
Skills.png
Skills.py

Daniel Roseman

unread,
Jul 18, 2019, 3:29:48 PM7/18/19
to Django users
On Thursday, 18 July 2019 15:56:59 UTC+1, Yves de Champlain wrote:
Indeed, it feels like I'm taking things the wrong way, maybe I coded too much PHP to be redeemable ...

My main problem might be coming from the fact that I need to parse the data to generate my view and can't simply pull the data from the DB.

Here are the models, template and view attached, as well as a screenshot of the result. 

Please note that as far as the logic of the app is concerned, "Competency" means the same as "Skill". I need to fix that dual terminology in the code. 

Thanks a lot for your answer.

yves

 The key is to think about the structure you need for output when building up your data in the view. In this case, what you want is a simple nested list which includes *all* the data for each row - including the skill object. (Also, there's no need to pad your lists with "X" and spaces; use bools.) In other words, the list looks like:

[
  [skill1, [True, False, False...],
  [skill2, [True, False, True...].
  ...
]

So, in the view:

for s in skills:
    v_skill = []
    valeur = False
    for a in achievements:
        ...
        if...
            valeur = True
        v_skill.append(valeur)
    v_table.append(skill, v_skill)

Now in the template you can simply do:

            {% for skill, achievements in skills %}
                  <tr
                  {% if skill.is_key %}
                    style="font-weight:bold;"
                  {% endif %}
                  >
                      <td style="text-align:right">{{ skill.code }}</td>
                      <td>{{ skill.name }}</td>

                      {% for achievement in achievements %}
                        <td style="text-align:center">
                          {{ achievement|yesno:"X, " }}
                        </td>
                      {% endfor %}
                  </tr>
                {% endfor %}

-- 
DR.

Yves de Champlain

unread,
Jul 18, 2019, 7:06:54 PM7/18/19
to Django users
That looks just perfect ! I knew but I see I still need to learn more about data structures in Python.

Thanks a lot !

yves
Reply all
Reply to author
Forward
0 new messages