Using a 2-dimensional array in a template

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Using a 2-dimensional array in a template

Yves de Champlain
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 !

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cf317fb9-affd-45d5-8901-9e6f15b70a1d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Using a 2-dimensional array in a template

Daniel Roseman-2
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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f4efbf39-e969-43f3-8cb2-6811eff0d06e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using a 2-dimensional array in a template

Yves de Champlain
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

Le jeudi 18 juillet 2019 10:24:04 UTC-4, Daniel Roseman a écrit :
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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bd32b282-ddbd-4950-a80b-fe8e0f5d0956%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Skills-models.py (3K) Download Attachment
Skills.html (2K) Download Attachment
Skills.png (256K) Download Attachment
Skills.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Using a 2-dimensional array in a template

Daniel Roseman-2
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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/add48121-770b-45e0-8550-a2e5ccbaecf1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using a 2-dimensional array in a template

Yves de Champlain
That looks just perfect ! I knew but I see I still need to learn more about data structures in Python.

Thanks a lot !

yves

Le jeudi 18 juillet 2019 15:29:48 UTC-4, Daniel Roseman a écrit :
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>{{ <a href="http://skill.name" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fskill.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFWJ8E5WfsZP8SyGASEKR3rIGO5OA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fskill.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFWJ8E5WfsZP8SyGASEKR3rIGO5OA&#39;;return true;">skill.name }}</td>

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

-- 
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a57a84d7-10fd-450c-804e-1562265b794f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.