Jinja2 - Loop over list to build a table - flask

I am quite new to Flask and I am having a hard time to understand why I am only getting a list of elements in my browser (single column), I would like to get 3 different columns and my data is correct:
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Mail</th>
<th>Delete</th>
</tr>
{% for n in customers %}
<tr>
<td>{{n['First Name']}} </td>
</tr>
<tr>
<td>{{n['Last Name']}}</td>
</tr>
<tr>
<td>{{ n['Phone']}}</td>
</tr>
<tr>
<td> Supprimer <td></td>
</tr>
{% endfor %}
</table>

What you are really looking for is something like this:
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Mail</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for n in customers %}
<tr>
<td>{{n['First Name']}} </td>
<td>{{n['Last Name']}}</td>
<td>{{ n['Phone']}}</td>
<td> Supprimer <td></td>
</tr>
{% endfor %}
</tbody>
</table>
tr Stands for Table Row. Check out this link to learn a bit more about tables

Related

Adding a new table row using HTMX

I am trying to add a new table row with Htmx. By clicking the "Add A New Row" button, a new row should be added to the table.
I can't get the row in the secondary_hx.html to be added to the table, instead it results as three forms next to each other without or .
Any help would be appreciated. Thank you in advance.
My code is like this:
main.html:
<table id="myTable">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody id="Tbody">
<tr>
<td>
<button class="add_button"
type="submit"
hx-post="/add_row/"
hx-target="#Tbody"
hx-swap="beforeend"
>
Add A New Row
</button>
</td>
</tr>
</tbody>
</table>
partials/secondary_hx.html:
<tr hx-target="this"
hx-swap="outerHTML">
<td>{{ form.a }}</td>
<td>{{ form.b }}</td>
<td>{{ form.c }}</td>
</tr>

Return the total of a group with Jinja

I have an object to return by group and then display the number of grouped items. example :
<table>
<thead>
<tr>
<th>Name</th>
<th>Total</th>
</tr>
</thead>
<tbody>>
{% set count = 0 %}
{% for item in data.items|groupby(attribute="name") %}
<tr>
<td>{{ item.grouper }}</td>
<td>{% for number in item.list %}{% set count = count + 1 %}{{ count }}{% endfor %}</td>
</tr>
{% endfor %}
</tbody>
</table>
I also tried :
<table>
<thead>
<tr>
<th>Name</th>
<th>Total</th>
</tr>
</thead>
<tbody>>
{% for item in data.items|groupby(attribute="name") %}
<tr>
<td>{{ item.grouper }}</td>
<td>{% for number in item.list %}{{ loop.length }}{% endfor %}</td>
</tr>
{% endfor %}
</tbody>
</table>
But the return for a total of 3 is: "111" or "333" and i want display "3",
avez-vous une idée ?
Be careful in jinja the "+" is for concatenation not for addition. For that there is a filter |add but right now if I understand what you want you don't need that:
<table>
<thead>
<tr>
<th>Name</th>
<th>Total</th>
</tr>
</thead>
<tbody>>
{% for item in data.items|groupby(attribute="name") %}
<tr>
<td>{{ item.grouper }}</td>
<td>{{ item.list|length }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Would be enough
<table>
<thead>
<tr>
<th>"Name"</th>
<th>"Total"</th>
<th>"%"</th>
</tr>
</thead>
<tbody>
{% set total = data.items|length %}
{% for item in data.items|groupby(attribute="name") %}
<tr>
<td>{{ item.grouper }}</td>
<td>{{ item.list|length }}</td>
<td>{{ total*(item.list|length)*0.1 }}{{ "%" }}</td>
</tr>
{% endfor %}
</tbody>
</table>

Django template table format

As I iterate results. There are rows that can be merged, however, relative columns will have multiple rows.
For example:
Currently:
Domain ID----sub Domain Title---- Sub domain ID
A1-----------------A1 title-----------A1.1
A1-----------------A1 title-----------A1.2
I would like it to be like this. With first two columns with merged rows
Domain ID----sub Domain Title---- Sub domain ID
A1----------------A1 title------------A1.1
---------------------------------------- A1.2
<table class="table table-sm">
<tr>
<th>Domain ID</th>
<th>Sub Domain title</th>
<th>Sub domain ID</th>
</tr>
{% for record in a6titles %}
<tr>
<td>A6</td>
<td>A6 title</td>
<td>titles: {{ record }}</td>
</tr>
{% endfor %}
</table>
Try this.
<table class="table table-sm">
<tr>
<th>Domain ID</th>
<th>Sub Domain title</th>
<th>Sub domain ID</th>
</tr>
{% for record in a6titles %}
<tr>
<td colspan=3>A6</td>
<td>titles: {{ record }}</td>
</tr>
{% endfor %}
</table>

how i can fill my data on just one bootstrap table django 2.1

my table looks like
date name place
4.09.2018 jack London
date name place
4.09.2018 ed paris
date name place
5.09.2018 sam istabul
i have problem with using table. i want to show one table my data but , it doesnt work, my program showing all my data different table but i dont want this. how i repair this situation ?
i want to show one header and all my data below from header like this
- date name place
4.09.2018 jack London
4.09.2018 ed paris
5.09.2018 sam istabul
but this situation adding header for all of my data. it shows here
my html codes
thank u for your interest...
<div class="container">
<div class="row">
<div class="col-3">
<a class="btn btn-warning" href="{% url 'ANASAYFA'%}"> ANASAYFA </a>
<br>
{% now "jS F Y H:i" %}
</div>
<div class="col-6">
{% for i in veriler %}
<table class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">TARİH</th>
<th scope="col">İSİM-SOYİSİM</th>
<th scope="col">VARDİYA BÖLGE</th>
<th scope="col">VARDİYA DÖNEM</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="i.row">{{ i.gun }}</th>
<th>{{ i.personel }}</th>
<th>{{ i.bolge }}</th>
<td>{{ i.vardiya_donemi }}</td>
</tr>
</tbody>
</table>
{% endfor %}
Try this:
<table class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">TARİH</th>
<th scope="col">İSİM-SOYİSİM</th>
<th scope="col">VARDİYA BÖLGE</th>
<th scope="col">VARDİYA DÖNEM</th>
</tr>
</thead>
<tbody>
{% for i in veriler %}
<tr>
<th scope="i.row">{{ i.gun }}</th>
<th>{{ i.personel }}</th>
<th>{{ i.bolge }}</th>
<td>{{ i.vardiya_donemi }}</td>
</tr>
{% endfor %}
</tbody>
</table>

django trying to put a table in a view

in my views.py:
def show(request):
query_results=Emails.objects.all()
#from py_utils import open_py_shell;open_py_shell.open_py_shell()
context = { 'query_result' : query_results }
return render(request, 'show.html', context)
in show.html:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
{% for item in query_result %}
<tr>
<td>Ohad</td>
<td>{{ item.email }}</td>
<td>{{ item.baseurl }}</td>
</tr>
{% endfor %}
</tbody>
</table>
an email object has email field and baseurl field but it seems that i cannot load it,
i checked and in query_result i have a list of Emails object just cannot put it in the table, any help?