I have a pythondict with the example format
data ={"X":{"0":1234, "1":5678, "2":1234}, "Y": {"0":4567, "1":1234,"2":4456}}
Unfortunately, I don't know how to iterate through the nested data in an HTML template to create a table.
Can you help me?
View
return render('results.html',
{'object': sorted(data.items())})
template
{% for key, value in object.items() %}
<tr>
<td> {{ key }}: </td> <td> {{ value }} </td>
</tr>
{% endfor %}
Related
say we have table in template
<table class="table">
<thead>
<tr>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
{% if {{student.academic_status}}=="promoted" %}
<td class=text-success>promoted</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
So is it possible using if statement in table in django-templates
Since you are already in a template tag, you should not use curly brackets ({{ … }}) for the variable, so:
{% if student.academic_status == "promoted" %}
…
{% endif %}
The correct way to add if statement in Django Templates
follow me !
if statement
{% if condition %}
{{here you add the key that you using in the views page exactly context}}
{% endif %}
I am trying to access data from a list of dictionary to my HTML template and the following error occured while trying to access the data from the dictionary. This same error even occured initially when I tried to loop using range(len(data)) but then I created another list that stores the range. But I can't do that for a LOD.
Could not parse the remainder: '[i]['fld_id']' from 'data[i]['fld_id']'
viwes.py
data_to_print=[
{
'fld_name':i,
'name':randomString(10),
'age':randomString(2),
'gender':randomString(5)
}for i in files_in_user_folder
]
for q in range(len(data_to_print)):
numbers.append(q)
return render(request, 'loginpage/datapage.html',{'data':data_to_print},{'rng':numbers})
template.html
</tr>
{% for i in rng %}
<tr>
<td> {{data[i]['fld_id']}} </td>
<td> {{data[i]['name']}} </td>
<td> {{data[i]['age']}} </td>
<td> {{data[i]['gender']}} </td>
<td> <a href="http://127.0.0.1:8000/media/{{data[i]['fld_id']}}.zip" download>Download</a></td>
</tr>
{% endfor %}
How can I resolve this?
in Django template you cannot use [] to parse anything, if its a proper dictionary use this instead
{{ data.i.fld_id }}
This is not how you loop through objects in a list (array) in python, you don't need the index at all (rng is not needed). You can just loop through data to iterate through the objects in the list (in this case each object is a person which is a dictionary). Then access the keys of the dictionary using the dot notation (see this to learn how to access python variables in django templates).
Do this instead:
{% for person in data %}
<tr>
<td> {{ person.fld_name }} </td>
<td> {{ person.name }} </td>
<td> {{ person.age }} </td>
<td> {{ person.gender }} </td>
<td> <a href="http://127.0.0.1:8000/media/{{ person.fld_name }}.zip" download>Download</a></td>
</tr>
{% endfor %}
Note: I'm using the key fld_name because that's what you defined in your code, as far as I can see fld_id isn't defined.
I want to display a number of the count in Django template in sequence.
Like if for loop iterate 5 time then it should print 0,1,2,3,4 in Django template.
users contains string which I pass from views.py
{% for user in users %}
<tr>
<td>
FOR LOOP ITERATE COUNT
</td>
<td>
{{ user.first_name }}
</td>
</tr>
{% endfor %}
You can user {{forloop.counter0}}:
{% for user in users %}
<tr>
<td>
{{forloop.counter0}}
</td>
<td>
{{ user.first_name }}
</td>
</tr>
{% endfor %}
I'm trying my best not to repeat myself in my code but I'm encountering a problem looping through a dictionary by key in my template.
I have two dicts:
exampledict={'firstkey':firstval, 'secondkey':secondval}
keys=['firstkey', 'secondkey']
keydict={'keys':keys}
In my template I want to loop over the exampledict using the keydict:
<tr>
{% for val in keydict %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
I've noticed this kind of combination of variables doesn't work at all, I tried by using:
{'firstkey':'firstkey'}
And sending that through to the template and later trying
{{ exampledict.firstkey }}
Is there a better way to accomplish what I'm trying to do here?
Edit 1:
Manually going through each key as:
<td> {{ exampledict.firstkey }} </td> <td> {{ exampledict.secondkey }} </td>
Where firstkey and secondkey is the actual dictkey for exampledict works, although it makes for a lot of repetition.
Edit 2:
views.py
def tabletest(request):
exampledict={'firstkey':'firstval', 'secondkey': 'secondval'}
keydict={
'keys':['firstkey', 'secondkey']
}
return render(request, 'MinaFakturor/tabletest.html', {'exampledict':exampledict, 'keydict':keydict})
template
<table>
<thead>
<tr>
{% for val in keydict.keys %}
<th>{{ val }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for val in keydict.keys %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
<tr>
<td>{{ exampledict.firstkey }}</td>
</tr>
</tbody>
</table>
Which produces this result:
If I remove the exampledict.firstkey term, nothing is produced in the table body.
I pass 2 groups of data from my view.py to my html file.
But only one group of data is shown in the webpage.
html:
<h3>Database: A</h3>
<table>
<tr>
<th>A</th>
<th>Counts A-1</th>
<th>Counts A-2</th>
<th>Value</th>
</tr>
{% load lookup %}
{% for i in Amatch %}
<tr>
<th> {{ Amatch|lookup:forloop.counter0 }} </th>
<td> {{ Amatchtotal|lookup:forloop.counter0 }} </td>
<td> {{ AmatchCount|lookup:forloop.counter0 }} </td>
<td> {{ Avalue|lookup:forloop.counter0 }} </td>
</tr>
{% endfor %}
</table>
<h3>Database: B</h3>
<table>
<tr>
<th>B</th>
<th>Counts B-1</th>
<th>Counts B-2</th>
<th>Value</th>
</tr>
{% load lookup %}
{% for i in Bmatch %}
<tr>
<th> {{ Bmatch|lookup:forloop.counter0 }} </th>
<td> {{ Bmatchtotal|lookup:forloop.counter0 }} </td>
<td> {{ BmatchCount|lookup:forloop.counter0 }} </td>
<td> {{ Bvalue|lookup:forloop.counter0 }} </td>
</tr>
{% endfor %}
</table>
view.py
return render(request, 'analysis/result.html', {'Amatch':Amatch, 'Amatchtotal':Amatchtotal, 'AmatchCount':AmatchCount, 'A_re':A_re, 'Avalue':Avalue, 'Bmatch':Bmatch, 'Bmatchtotal':Bmatchtotal, 'BmatchCount':BmatchCount, 'B_re':B_re, 'Bvalue':Bvalue,})
Two groups of data are supposed to be shown on the page as I expected.
However, only the data from group B is shown on the page.
I tried to switch the order of group A and group B, but still only group B is shown.
I'm pretty sure data are passed successfully by checking them in terminal.
Are there anything wrong with my code?