I want to pass the following dictionary as context to Django template:
context = {'prices': prices, 'listings_links': listings_links, 'listings_names': listings_names, 'photo_links': photo_links}
The dictionary's values are lists.
In the template I want to display those lists as columns in HTML table. However I am not sure how to further develop the following skeleton table code:
<table>
<tr>
<th>Price</th>
<th>Link</th>
<th>Listing name</th>
<th>Photo link</th>
</tr>
{% for loop start here? %}
<tr>
<td> {{prices[0] }} </td>
<td> {{ listings_links[0] }} </td>
<td> {{ listings_names[0] }} </td>
<td> {{ photo_links[0] }} </td>
</tr>
#next rows go here...
{% endfor %}
</table>
In the view, zip your lists into a single iterable.
items = zip(prices, listings_links, listings_names, photo_links)
context = {'items': item}
Then you can unpack items in the template:
{% for price, listing_link, listing_name, photo_link in items %}
<tr>
<td>{{ prices }}</td>
<td>{{ listing_link }}</td>
<td>{{ listing_name }}</td>
<td>{{ photo_link }}</td>
</tr>
{% endfor %}
Related
so I have some data being returned via API which I am passing into my template I am having an issue though in displaying the data as currently it is showing each char per row, I am trying to make it so in the table I have the coin name in a row with the value in the next col and so on.
Data being returned-
{"error":[],"result":{"ZGBP":"30622.0790","DASH":"0.5104491200","ADA":"2473.80445621","ZUSD":"67787.8285","KSM":"24.7142610000","CHZ":"13773.0349000000","XXLM":"6926.27220000","KNC":"0.0000000000","MATIC":"1838.9295772000","ZRX":"0.0000000000","BAL":"0.0000000000","XXDG":"17006.92601155","LINK":"144.2407000000","USDT":"60000.00000000","TRX":"923.80015900","COMP":"0.0000034600","ENJ":"257.6815000000","DOT":"0.0000000000","XLTC":"11.4923900000","SC":"0.0000000200","XZEC":"0.0000073100","SOL":"1133.3543869800","SUSHI":"172.4585500000","XXRP":"0.00000000","XETH":"14.5877343640","AAVE":"83.6218990800","ATOM":"151.26763831","XXBT":"0.0000012880","ALGO":"32063.69514500","OCEAN":"652.6077000000"}}
My template-
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Amount</th>
</tr>
</thead>
<tbody>
{% for v in api_reply %}
<tr>
<td>{{ v }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Probably you get the string object instead of json, but if it is a json...
In template use this:
<tbody>
{% for k,v in api_reply['result'].items() %}
<tr>
<td>{{ k }}</td>
<td>{{ v }}</td>
</tr>
{% endfor %}
</tbody>```
I am using Django and in the front end, I am trying to show only "Reorder" Parts on the table. To define the status of Available and Reorder, I am normally using the if-else method to track. I was thinking about filtering but I have no such Status field in my DB. Anyone can give me any idea how to do that? Here is what I've done for now
HTML
<tr>
<th class="serial">#</th>
<th>Part No</th>
<th>Part Name</th>
<th>Quantity</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>
{% if parts %}
{% for part in parts %}
<tr>
<td class="serial">{{ forloop.counter }}</td>
<td>{{ part.partno }}</td>
<td>{{ part.partname }}</td>
<td>{{ part.quan }}</td>
<td>
{% if part.quan <= part.limit %}
<p style="color: #FF0000">
Reorder
</p>
{% elif part.quan > part.limit %}
<p style="color:#008000">
Available
</p>
{% endif %}
</td>
<td>{{ part.created_date }}</td>
</tr>
Part Table
You can reference an value from the instance in a filter, this queryset will return your parts that need reorder
from django.db.models import F
parts = Part.objects.filter(quan__lte=F('limit'))
I'm trying to include a template inside a for loop with a batch filter applied
But I can't figure out how/if I can include for each object in the list that the filter returns
I see people online selecting from the loop like so:
{% for result in results %}
<tr>
<td>{{ result[0] }}</td>
<td>{{ result[1] }}</td>
<td>{{ result[2] }}</td>
</tr>
{% endfor %}
I just can't figure out how to include for each in the list
My code is as follows:
{% for post in posts | batch(2, ' ') %}
<tr>
<td style="Width: 10%; height: auto">
{% include '_post.html' %}
</td>
</tr>
{% endfor %}
Including portions of a template in a loop/filter construction is perfectly fine.
Using your example, to build a table of posts using a partial template for each batch, you'll need:
The template with the loop/filter where you include the partial:
<table>
<thead><tr>
<th>Title</th>
<th>Author</th>
<th>Title</th>
<th>Author</th>
</tr></thead>
<tbody>
{% for row in posts | batch(2) %}
{% include "row.html" %}
{% endfor %}
</tbody>
</table>
The partial template "row.html":
<tr>
<td>{{ row[0].title }}</td>
<td>{{ row[0].author }}</td>
<td>{{ row[1].title }}</td>
<td>{{ row[1].author }}</td>
</tr>
Another option would be to iterate over the batch partition again and use a simpler partial template:
The template:
<table>
<thead><tr>
<th>Title</th>
<th>Author</th>
<th>Title</th>
<th>Author</th>
</tr></thead>
<tbody>
{% for row in posts | batch(2) %}
<tr>
{% for col in row %}
{% include "col.html" %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
And "col.html":
<td>{{ col.title }}</td>
<td>{{ col.author }}</td>
If it's not working, double check the variable names, partial names, etc.
I would like show element in my list on template html like that :
<table class="table table-striped col-md-12">
<tbody>
{% for row in list %}
<tr>
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ ..... }}</td>
</tr>
{% endfor %}
</tbody>
When the len(list) evolve, I need to call new row manually.
So to have a generic call and dependent of the length on my list I choice the method forloop.counter0, but when I write my code, I have error or no elements on my screen.
<tr>
{% for i in "xxx" %}
<td>{{ row.forloop.counter0 }}</td>
{% endfor %}
</tr>
You shouldn't need to look up the index at all, just have another forloop inside that loops over the row
{% for i in row %}
<td>{{ i }}</td>
{% endfor %}
If you need anything more granular, you might want to look into making an actual model to represent the data, and then iterate over a list of this model.
I would like to print information about all my users and their groups in a template, like this:
{% for user in users %}
<tr>
<td>(there should be enumerate here - 1,2,3,4 etc...)</td>
<td>{{ user.last_name }}</td>
<td>{{ user.first_name }}</td>
<td>
{% for group in user.groups %}
{{ group }}, </td>
{% endfor %}
</tr>
{% endfor %}
but it doesn't work:
'ManyRelatedManager' object is not iterable
I have two additional questions:
1. How can I easily enumerate users, like this:
<tr>
<td>1</td>
<td>Kowalski</td>
<td>John</td>
...
</tr>
<tr>
<td>2</td>
<td>Smith</td>
<td>John</td>
...
</tr>
...
2. How can I print groups like this:
group1, group2, group3
instead
group1, group2, group3,
(last comma is wrong)
Thank you very much.
you should change
{% for group in user.groups.all %}
{{group}}
The following code should do everything you are asking for:
{% for user in users %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ user.last_name }}</td>
<td>{{ user.first_name }}</td>
<td>
{% for group in user.groups.all %}
{{ group }}
{% if not forloop.last %},{% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
Check out the following link from the official docs for more information about builtin forloop variables: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for