Reputation: 45
I have a dictionary with 6 keys and each key's value is a list with 100 elements
my_dict = {"Key1":["name1", "name2", ..., "name100"],
"Key2":["id1", "id2", ..., "id100"],
...
"Key6":["type1", "type2", ..., "type100"]}
I am trying to make a table that is 100 x 6, where each column will be a list. I have tried everything in the template and come up short every time. Is there some simple concept I am missing? Should I be manipulating the data in the backend to make it more manageable?
Edit: I probably should be more clear. I can render my template fine, I can put stuff in a table, just not how I want to.
I can do things like
<tr>
{% for i in my_dict.items %}
<td>{{i}}</td>
{% endfor %}
</tr>
Giving me 1 row with 6 columns
or
{% for items in dict.items %}
<tr>
{% for item in items %}
<td>{{item}}</td>
{% endfor %}
</tr>
{% endfor %}
giving me 6 rows and 100 columns
I could do
{% for item in dict.Key1 %}
<tr>
<td>{{item}}</td>
</tr>
{% endfor %}
Giving me 1 column and 100 rows
But I need each item, in each list, in its own column.
Upvotes: 1
Views: 226
Reputation: 133
If I am correct, your table needs to have 6 columns and 100 rows right? If so it is difficult to do that in the template, and I would edit the data before sending it to the template. Usually you should always try to keep complicated logic like that outside of the template anyways.
View:
def get_context_data(self, **kwargs):
context = super(YOUR_CLASS_HERE, self).get_context_data(**kwargs)
# I defined each list this way just to test
my_dict = {
"Key1": ["name1", "name2", "name100"],
"Key2": ["id1", "id2", "id100"],
"Key6": ["type1", "type2", "type100"]
}
my_data = [[v[i] for k, v in my_dict.items()] for i in range(3)] # This would be range(100) for you, once again I was just testing.
context['my_data'] = my_data
return context
And in the template:
<table>
<thead>
</thead>
<tbody>
{% for row in my_data %}
<tr>
{% for entry in row %}
<td>{{ entry }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Edit: I believe this solution solves your problem in the best way. Granted it does the work in the view rather than the template, but you don't want to do this type of data manipulation in the template. This solution changes the data with 1 line of code:
my_data = [[v[i] for k, v in my_dict.items()] for i in range(100)]
It's much easier to do it in python than with the template tags in the template. I can't think of a way to do it in the template without writing a custom template tag. If you do use a custom template tag, then your still using python so you should have just done it in the view.
Upvotes: 2
Reputation: 1155
May be you are asking for this (how to render this dict on template side). Try this,I hope, I got your question right as it is not clear much otherwise make me correct.
{% for key, value_list in my_dict.items %}
# your stuff
{% for value in value_list %}
# more stuff here (2)
{% endfor %}
{% endfor %}
Upvotes: 0