For loop Count in django template - python

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 %}

Related

Python dict into HTML-template (Django)

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 %}

Using while loop with counter in django template

I need to put a counter in while loop in template. So I did:
<tbody>
{% with count=1 %}
{% while count <={{orders_count}}: %}
{% for order in orders %}
<tr>
<td style="width:5%;"></td>
<td>{{count}}</td>
<td>{{order.name}}</td>
</tr>
{% count+=1 %}
{% endfor %}
{% endwhile %}
{% endwith %}
</tbody>
But finaly I have the following error:
Invalid block tag on line 216: 'while', expected 'endwith'. Did you forget to register or load this tag?
You do not need a while loop here, you can simply make use of the |slice template filter [Django-doc]:
<tbody>
{% for order in orders|slice:orders_count %}
<tr>
<td style="width:5%;"></td>
<td>{{ forloop.counter }}</td>
<td>{{ order.name }}</td>
</tr>
{% endfor %}
</tbody>
But slicing, etc. does not really belong in the template. You normally do that in the view.
You can try this :
<tbody>
{% for order in orders %}
<tr>
<td style="width:5%;"></td>
<td>{{forloop.counter}}</td>
<td>{{order.name}}</td>
</tr>
{% endfor %}
</tbody>

Could not parse the remainder: '[i]['fld_id']' from 'data[i]['fld_id']'

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.

Jinja lookup key in for loop

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?

Django favourite/compare feature

Just wondering if Sessions can be used to create a quick compare view of two products on my Django app. I'm listing items for sale and would like a user to be able to 'like' multiple items then have a new view to compare the selected products. Any thoughts?
Thanks
Sure, just assign the list products to a session variable.
Then assign the products list to the template, which could look something like that:
<table>
<tr>
<td></td>
{% for product in products %}
<th>{{ product.title }}</th>
{% endfor %}
</tr>
<tr>
<th>Feature 1</th>
{% for product in products %}
<td>{{ product.feature1 }}</td>
{% endfor %}
</tr>
<tr>
<th>Feature 2</th>
{% for product in products %}
<td>{{ product.feature2 }}</td>
{% endfor %}
</tr>
</table>

Categories