Django template loop through API results to display in table - python

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

Related

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

django: View to delete html table row not taking effects in Database

I have a page that displays rows from a db table. I added a button to each row that is designed to delete the row upon its click as well as redirect toward a deleted confirmation page. Everything seems to work, unless that nothing gets deleted in the database table.
Here is what my views.py look like:
def ProductsView(request):
queryset = Products.objects.all()
products_list = list(queryset)
request.session['context'] = products_list
#table = ProductsTable(queryset)
#context = {'table':table}
return render(request, 'inventory.html', {'products_list':products_list})
def DeleteProduct(request, Id):
Products.objects.filter(ProductId=Id).delete()
return render(request,'delete_confirmation.html')
and here is what the html page look like, including the code for the delete button
<table class="table text-center table-bordered table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">NAME</th>
<th scope="col">CATEGORY</th>
<th scope="col">TAG NUMBER</th>
<th scope="col">STOCK ON HAND</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody style="background-color: white">
{% for Products in products_list %}
<tr>
<td>{{ Products.ProductId }}</td>
<td>{{ Products.ProductName }}</td>
<td>{{ Products.ProductCategory }}</td>
<td>{{ Products.ProductTagNumber }}</td>
<td>{{ Products.StockOnHand }}</td>
<td>
<a class="btn btn-primary" href="{% url 'delete_confirmation' Id=Products.ProductId %}" style="color: white">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
here is what urls.py looks like:
path('inventory.html', ProductsView, name="inventory"),
path('delete_confirmation/<int:Id>', DeleteProduct, name="delete_confirmation"),
and when I print Id in console inside of the ``DeleteProduct``` view, it ouputs the correct Id value
I can't find out what I am doing wrong, I have been at it all day and need a pair of fresh eyes to look at it! Does anyone has a clue on what is wrong here?

Loop to render Django view context as table in the template

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

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>

Flask/Jinja2 template: Loop with multiple list positions

[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).

Categories

Resources