Flask/Jinja2 template: Loop with multiple list positions - python

[Not really sure if the topic makes sense, but didn't found a more meaningful one.]
I've created a template which looks like:
{% for x in jobs %}
<table>
<tr>
<td></td>
<td>{{ x.Ecordov.oovorder }}</td>
</tr>
<tr>
<td></td>
<td>{{ x.ooaname1.split('{}')[0] }}</td>
</tr>
<tr>
<td></td>
<td>{{ x.ooaname2.split('{}')[0] }}</td>
</tr>
<tr>
<td></td>
<td>{{ x.ooazusatz.split('{}')[0] }}</td>
</tr>
</table>
{% endfor %}
As you can see, I'm getting a specific position in multiple lists, which works quite well.
The problem I'm trying to solve: These lists have up to 16 positions, which I have to render. I could of course copy/paste the above <tr> </tr> block 16 times into the template, and edit the line positions, but I'm quite sure that there is a better, more automated, way; however, I wasn't able to find this out on my own up until now.
Could anyone point me into the correct direction?
Thanks for any help and all the best!

Try this:
{% for x in jobs %}
{% for i in range(0, 17) %}
<table>
<tr>
<td></td>
<td>{{ x.Ecordov.oovorder }}</td>
</tr>
<tr>
<td></td>
<td>{{ x.ooaname1.split('{}')[i] }}</td>
</tr>
<tr>
<td></td>
<td>{{ x.ooaname2.split('{}')[i] }}</td>
</tr>
<tr>
<td></td>
<td>{{ x.ooazusatz.split('{}')[i] }}</td>
</tr>
</table>
{% endfor %}
{% endfor %}
If you don't know how many elements does the list have you have to find it first and use it as the stop argument (the second argument of the range() function).

Related

Generating new HTML rows for each Document from firestore in python Django

I have a collection of documents in a Firestore database. I want to create a web form to display all the documents and their fields. I started by streaming all the documents using:
docs = db.collection(u'users').stream()
and then appending all the docs IDs to a list and pass it to the HTML file as follows
def index(request):
myIDs =[]
db = firestore.Client()
docs = db.collection(u'users').stream()
for doc in docs:
myIDs.append(f'{doc.id}')
return render(request, 'index.html', {
"weight":"abc",
"firstRow":myIDs[0],
"secondRow":myIDs[1],
"thirdRow":myIDs[2],
"fourthRow":myIDs[3],
"fifthRow":myIDs[4],
"sixthRow":myIDs[5],
"seventhRow":myIDs[6],
})
After that, I created a very basic HTML code just to display the document IDs as follows:
<html>
<body>
<table border="1" cellpadding = "5" cellspacing="5">
<tr>
<td>{{ firstRow }}</td>
<tr>
<td>{{ secondRow }}</td>
</tr>
<tr>
<td>{{ thirdRow }}</td>
</tr>
<tr>
<td>{{ fourthRow }}</td>
</tr>
<tr>
<td>{{ fifthRow }}</td>
</tr>
<tr>
<td>{{ sixthRow }}</td>
</tr>
<tr>
<td>{{ seventhRow }}</td>
</tr>
</tr>
</table>
</body>
</html>
Till this point I am just doing very basic stuff. But I am new to Django and Python for the Web, so my question is: how can I make it so that the HTML rows become adaptive based on the number of documents?
For example, if I have 20 documents, I want to make it so that it will generate the 20 rows and pass each of the document IDs to one row, and so on.
This is fairly simple in Django
You can pass the list MyIDs as a context variable and let your template do the heavy lifting
return render(request, 'index.html', {
"weight":"abc",
"myIDs": myIDs
})
Then in your template
<table border="1" cellpadding = "5" cellspacing="5">
{% for id in myIDs %}
<tr>
<td>{{ id }}</td>
</tr>
{% endfor %}
</table>
(I suspect you can make this even simpler by passing docs as a context variable, which would let you do
{% for doc in docs %}
<tr>
<td>{{ doc.id }}</td>
<td>{{ doc.title }}</td>
</tr>
{% endfor %}
</table>
but I'm not 100% familiar with the output of stream() )

Django template loop through API results to display in table

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>```

Show only "Reorder" item

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'))

Jinja2 include with batch filter

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.

Issue rendering with nested tables in Django

I'm trying to loop two tables within each other. I have a list of tasks that each have a list of materials. I'd like to next the tables within each other. However, the code below doesn't render the second task into columns. So, when it loops it seems as if the table structure is destroyed. I'm using Django/Python.
<div class="table-responsive">
<table id="taskTable" class="table">
{% for task in projecttype.projecttask_set.all %}
<h5>Task - {{ task.name }}</h5>
<thead class="alert-success">
<tr>
<th>Quantity</th>
<th>Units</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ total_units_required }}</td>
<td>{{ task.base_unit_label }}</td>
<td>{{ task.base_unit_cost }}</td>
</tr>
</tbody>
<table id="materialTable" class="table">
<thead>
<tr class="alert-info">
<th>Material</th>
<th>Cost per Task Unit</th>
<th>Material Cost</th>
<th>Total Cost</th>
</tr>
</thead>
{% for material in task.materials.all %}
<tbody>
<tr>
<td>{{ material.name }}</td>
<td>{{ material_quantity_per_task_base_unit }}</td>
<td>{{ total_material_quantity }}</td>
<td>{{ total_material_cost }}</td>
</tr>
</tbody>
{% endfor %}
</table>
{% endfor %}
</table>
</div>
I encountered the same issue. The nested table has to be inside a table cell
e.g.:
<table>
<tr>
<td>
<table>
...
</table>
</td>
</tr>
</table>

Categories

Resources