How to Display Python Flask DB entires in Jinja 2 html table - python

I currently have a DB whose table I want to review inside of my web app via a specific route.
I want to view the Jinga2 output of the entires in the DB in a easy to read table via the app.
What I currently have works but it shows all the entries in one line instead of a table format like an excel spreadsheet.
My code is below:
{% for row in rows %} <tr>
<td>{{ row.id }}</a></td>
<td>{{ row.question }}</td>
<td bgcolor="{{ row.stat_colour }}">{{ row.unit_status }}</td> </tr> >
{% endfor %}

For anyone having similar issues, I was able to get it to work by using the code block below for my html:
<html !DOCTYPE>
<head>
<title>SQLite 3 DB Questions Table</title>
</head>
<body>
<table>
{% for row in rows %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.question }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>

Put the opening tr before the start of the loop
<tr> {% for row in rows %}

Related

Need help in Django Templates(using forloops)

I have 3 models namely Uses, Customer and Services. I have a query whose result is stored in the variable count.
This is the query
counts = Services.objects.filter(uses__customer=customer).annotate(times_used=Count('uses'))
Each count[x].times_used has a variable,x ranges from 0 to infinity.
I have used a for loop in my html to get all the Services which I have passed through context in views.
Now I need to get unique value of count for each service. How can I do this.
Here is my html code
<table class='table table-bordered' id='dataTable' width='100%' cellspacing='0'>
<thead class='bg-primary'>
<tr class='text-white'>
<th>Service-Id</th>
<th>Service-name</th>
<th>Count</th>
<th>Price</th>
</tr>
</thead>
<tbody class="text-dark">
<tr>{% for service in services %}
<td>{{ service.id }}</td>
<td>{{ service.service_name }}</td>
<td>#code for printing count[x],times_used#</td>
<td>{{ service.price }}</td>
</tr>{% endfor %}
</tbody>
</table>
{% for elem_1 in results_1 %}
{{elem_1}}
{% for elem_2 in results_2|index_filter:forloop.counter0 %}
{{elem_2}}
{% endfor %}
{% endfor %}
You can use that structure in your template, anywhere.
You use a builtin filter "index_filter".
Basically, when myvar is piped with | to index_filter:forloop.counter0, then the function will call the element in results_2 with index = forloop.counter0. This later counter being the one of the parent for loop.

Update specific cell via python in html

im making a website that displays an object from the database with associated items(foreginKey). As of now i list the components in a table and it works fine. Also works with refresh to update the values. But i want only the cell with the dynamic value to update without calling the refresh to reload the whole page to stop flickering images etc.
Havent really tried much as im not so good at this, tho i figured that i need som "id" on the to make it targetable perhaps? Also i think i need to put the .value outside the initial for loop and maybe loop it seperatly inside SetInterval function. But how do I do that?
I am using Django 2.1.4 / Python 3.7.1 / Win10 / MSSQL running on localhost at the moment.
Also my first question here so hope the formatting is good.
<h2>{{ object.name }}</h2>
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th>Description</th>
<th>Name</th>
<th>Fabrikat</th>
<th>Type</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for komponent in object.komponent_set.all %}
<tr>
<td>{{ komponent.beskrivning }}</td>
<td>{{ komponent.namn }}</td>
<td>{{ komponent.fabrikat }}</td>
<td>{{ komponent.typ }}</td>
<td>{{ komponent.value }}</td>
</tr>
{% endfor %}
<script>
var myVar = setInterval(valueRefresh, 5000);
function valueRefresh() {
{% for komponent in aggregat.komponent_set.all %};
<td>{{ komponent.value }}</td>;
{% endfor %};
}
</script>
</tbody>
</table>
</div>
Sorry if this is rookie hour, but i dont know how to go about this.

Multicolumn table in django template

How to fill multicolumn table in django template?
I have list of n elements (let's say just numbers there) and I want to create a table (looks like grid) with 5 cells in a row.
Very easy to create table of 1 column, but to create 5? Of course we assume than list may have arbitrary number of items from zero to couple hundreds.
<tbody>
{% for item in data.items.all %}
<tr class="item-{{ item.number }}">{{ item.number }}</tr>
{% endfor %}
</tbody>
Try the following code:
View
data = [1,2,3,4,......,99,100]
Template
<table border="1">
<tbody>
<tr>
{% for item in data %}
{% if forloop.counter|divisibleby:5 %}
<td>{{ item }}</td>
</tr>
<tr>
{% else %}
<td>{{ item }}</td>
{% endif %}
{% endfor %}
</tr>
</tbody>
</table>
This code checks the loop count each time. if it is divisible by 5 then close the current tr tag and add a new one.
This may not be the right way to achieve this.
Hope that helps.

Flask Template - For Loop Iteration key:value

I've got an HTML template with a Flask Jinja for loop in it which generates a table and looks like:
<tbody>
{% for segment in segment_details %}
<tr>
<td>{{segment}}</td>
<td>{{segment_details['{{segment}}']}}</td>
</tr>
{% endfor %}
</tbody>
I'm trying to iterate through a document of varying length/keys and present each row in the table as the key and value. In my Python code I've got this which has the desired response in the shell:
for item in segment_details:
print(item, segment_details[item])
But in Flask I get the item correctly listing all the rows but the
{{segment_details['{{segment}}']}}
Isn't producing any values, I've tried with and without the single quotes. Is this possible?
This is where your error is:
<td>{{segment_details['{{segment}}']}}</td>
There is no need for the {{ }} inside.
It should be just:
<td>{{segment_details[segment]}}</td>
For more see the documentation for Jinja.
When you are writing a statement(if, for) in Jinja2 you use {% statement %} but when you are accessing a variable then just use {{ variable }}.
it is a solution
<tbody>
{% for key, segment in segment_details.items() %}
<tr>
<td>{{ key }}</td>
<td>{{ segment }}</td>
</tr>
{% endfor %}
</tbody>

Django - Use forloop.counter0

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.

Categories

Resources