Django For loop template for dict data - python

So in Python if i have a Dictionary rather than a list with a dataset, I would normally call (for example):
for i in myDictionary:
myDictionary[i]['event']
myDictionary[i]['timestamp']
etc.
But in Django this does not work:
{% for i in myDictionary %}
<tr>
<td> {{ myDictionary.i.event }}</td>
<td> {{ myDictionary.i.timestamp }}</td>
</tr>
Does not produce results - what's the optimal way to handle dictionaries with an arbitrary number of keys?

You have a weird structure. What you want is a list of dictionaries.
myDicts = []
for i in myDictionary:
data = {'event': '...', 'timestamp': '...'}
myDicts.append(data)
Then, you can loop over dictionary values by using the function .items, like in a normal loop (but don't include the parenthesis):
{% for elem in myDicts %}
{% for key, value in elem.items %}
<tr>
<td> {{ myDictionary.key }}</td>
<td> {{ myDictionary.value }}</td>
</tr>
{% endfor %}
{% endfor %}

Related

Iterating over a list from an object in Django template

I am trying to iterate over a list that is below an object in a Django template.
Unfortunately I am not getting anywhere.
This is the object:
{'Value1': [8, 5, 4, 7, 4, 5], 'Value2': [], 'Value3': []}
I am doing in the template something like:
{% for entry in data %}
<tr>
<td>{{ entry }}</td>
<td>{{ entry.0 }}</td>
</tr>
{% endfor %}
or
{% for entry in data %}
<tr>
<td>{{ entry }}</td>
{% for i in entry %}
<td>{{ i }}</td>
{% endfor %}
</tr>
{% endfor %}
But all I am getting is just the first letter of the key.
You have to look for both - keys and values in dict's items:
{% for key, value_list in data.items %}
<tr>
<td>{{ entry }}</td>
{% for value in value_list %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
Did you try iterating over the items in your dict, like so?
{% for key, value in data.items %}

How to go through two loops in django templates

Can anyone tell me what am I doing wrong in this code:
{% for dayName in data %}
<tr>
<td>{{ dayName }}</td>
{% for value in data.dayName %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
data is an object containing arrays, for an instance:
data['Sunday'] = [1 ,2 ,3]
And all what I want to do is create two loops through that object.
I will be thankful for each form of help,
Thanks in advance
dayName is a variable not the key itself. data.dayName is interpreted as data['dayName'], that's why you're not getting the right results.
Instead, you can do:
{% for dayName, vals in data.items %}
<tr>
<td>{{ dayName }}</td>
{% for value in vals %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}

jinja2: building table by iterating down columns instead of across rows

If I have the following lists:
headers = ['Name', 'Age']
rows = [['Johnny', 30], ['Zack', 20]]
I can easily make a table via Jinja2 (https://jsfiddle.net/equbh9du/1/):
<table class="table table-bordered table-hover">
<thead>
<tr>
{% for h in headers %}
<td>{{ h }}</td>
{% endfor %
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
I'm noticing it's much easier (and more organized) to return my data as a dictionary:
d = {'Name': ['Johnny', 'Zack'], 'Age': [30, 20]}
Is there an easy way to build the table I built above using this dict. I imagine I need to finish the iteration down each column before continuing to the next column (in the example above I finish the iteration across each row before continuing to the next row).
This is the code I have so far but I'm getting a messed up table (https://jsfiddle.net/j164fqy9/1/):
<table class="table table-bordered table-hover">
<thead>
<tr>
{% for h in d %}
<td>{{ h }}</td>
{% endfor %
</tr>
</thead>
<tbody>
{% for h, col_values in d.items() %}
{% for item in col_values %}
<tr>
<td>{{ item }}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
EDIT: If HTML standard prevents iterating down columns first, then I need to construct the headers and rows from d. Is below the best way to do this?
headers = [h for h in d]
rows = [[l[i] for h, l in d.items()] for i in range(len(d['Name']))]
If you allways have the same number of items in col_values this should work fine :
{% for i in range(d['Name']|count) %}
<tr>
{% for k in d %}
<td>{{ d[k][i] }}</td>
{% endfor %}
</tr>
{% endfor %}

Python-Django: user info in template

I have database, currently from my database its displaying like this in my Template(HTML table) page.
sno--scene--frames--empID
1------001----24-----100
2------002----34-----100
3------003----20-----101
4------004----15-----101
5------005----10-----100
But i want to display like this(below). How to get this from HTML(tables). I am using Python-Django.
sno---scene---frames---empID
1--------001-----24-------100
2--------002-----34-------100
3--------005-----10-------100
------------- tot=68
1------003-----20--------101
2------004-----15--------101
-------------tot=35
You should prepare the data in a view - group them by id, calculate sum. Result cal looks like this:
items_info = [{'items':[item1, item2, item3], 'total': 68}, {'items':[item4, item5], 'total': 35}]
Then template should be like this:
{% for info in items_info %}
{% for item in info.items %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ item.scene }}</td>
<td>{{ item.frames }}</td>
<td>{{ item.id }}</td>
</tr>
{% endfor %}
<tr>
<td colspan="3">TOTAL</td>
<td>{{ info.total }}
</tr>
{% endfor %}
Hope this helps!

using list index lookup within a template loop in django

Basically, what I want to do is have the template system loop through two independent lists to fill two columns of a table. My approach was to use an index list (numList) as a way to access the same index of the two lists. I tried using the dot notation for list index lookup within a template loop but it doesn't seem to work in the loop. Any ideas on how I can remedy this?
numList = [0, 1, 2, 3]
placeList = ['park', 'store', 'home', 'school']
speakerList = ['bill', 'john', 'jake', 'tony']
<table>
<tr>
<th>Location</th>
<th>Time</th>
<th>Speaker</th>
</tr>
{% for num in numList %}
<tr>
<td>{{ placeList.num }}</td>
<td>1:30</td>
<td>{{ speakerList.num }}</td>
</tr>
{% endfor %}
</table>
The easiest thing is probably to combine your lists in python and then just look through the combined list in the template:
combinedList = [(placeList[i],speakerList[i]) for i in range(4)]
{% for entry in combinedList %}
<tr>
<td>{{ entry.0 }}</td>
<td>1:30</td>
<td>{{ entry.1 }}</td>
</tr>
{% endfor %}
Or for transparency, you could make combinedList a list of objects or dictionaries for example:
combinedList = [{'place':placeList[i],'speaker':speakerList[i]} for i in range(4)]
{% for entry in combinedList %}
<tr>
<td>{{ entry.place }}</td>
<td>1:30</td>
<td>{{ entry.speaker }}</td>
</tr>
{% endfor %}
You could aggregate these two lists in one.
For example:
yourlist = [('park','bill'),('store','john'),('home','jake'),...]

Categories

Resources