I need to compare postback.campaign_id with the value of the next item. If it's similar, then, I don't print nothing, if not, then, print my row.
But, this code outputs everything.
<table>
<tr>
<td class="expand expand-campaign"><a>Test</a></td>
<td>{{ account_id }}</td>
</tr>
{% for postback in postbacks %}
{% if loop.nextitem is defined and postback.campaign_id != loop.nextitem[0] %}
<tr>
<td></td><td></td>
<td><input type="checkbox"></td>
<td class="expand expand-site"><a>{{ postback.campaign_id }}</a></td>
</tr>
{% endif %}
{% endfor %}
</table>
The loop.nextitem contains the whole variable in your next item. So, if it is a dictionary or object, like in your case, you can access its properties like the current object under loop.
In your case that would mean:
postback.campaign_id != loop.nextitem.campaign_id
You also have the issue that your last item will always be skipped by the condition loop.nextitem is defined. Since you are going to always print the last item of a streak of similar items, you can perfectly do:
{% if loop.nextitem is undefined
or postback.campaign_id != loop.nextitem.campaign_id
%}
Also note that, there is a loop.changed() construct in Jinja that can be handy in this specific use case.
So, with a list like:
postbacks = [
{'campagn_id': 1},
{'campagn_id': 2},
{'campagn_id': 2},
{'campagn_id': 3},
{'campagn_id': 3},
]
And this snippet of Jinja:
<table>
{%- for postback in postbacks %}
{%- if loop.changed(postback.campaign_id) %}
<tr>
<td class="expand expand-site">
{{ postback.campaign_id }}
</td>
</tr>
{%- endif %}
{%- endfor %}
</table>
It renders, with some style added for clarity, as:
table {
border-collapse: collapse;
}
td {
border: 1px solid;
padding: 4px;
}
<table>
<tr>
<td class="expand expand-site">
1
</td>
</tr>
<tr>
<td class="expand expand-site">
2
</td>
</tr>
<tr>
<td class="expand expand-site">
3
</td>
</tr>
</table>
Related
I would to change the color of cell in table depending on condition. How could I do that?
This is my code:
HTML page:
<table style="margin: 3px">
<thead>
<tr style="background-color: #7cc3a97d">
<th class="text-center">Query/File name</th>
<th class="text-center">Cosine similarity</th>
</tr>
</thead>
<tbody>
{% for key, value in cosineResults.items() %}
<tr>
<td> {{ select_query|safe }} / {{ key|safe }} </td>
{% if value|float > 0.7 %}
<td class="bg-danger"> {{ value|safe }} </td>
{% elif value|float > 0.3 %}
<td class="bg-warning"> {{ value|safe }} </td>
{% else %}
<td>{{ value|safe }}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
It always takes the first condition (> 0.3) even if the value is smaller.
Thanks in advance
I am developing a Django application about statistics calculations. But now I have faced a problem that I can't solve. I have two lists in my views.py list1 = [5, 6, 7, 8] list2 = [5, 6, 7, 8]. I sent this to Django templates and I also sent 'n' : range(7) as context.
In my html code, there have a code
<table>
<thead>
<tr>
<th>list1</th>
<th>list2</th>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</thead>
</table>
Now I want to print first value of each list in first row, then second value of each list in second row and so on.
So I have made a code something like this
<table>
<thead>
<tr>
<th>list1</th>
<th>list2</th>
</tr>
{% for i in n %}
<tr>
<td> {{ list1.i }} </td>
<td>{{ list2.i }}</td>
</tr>
{% endfor %}
</thead>
</table>
After writting this code, I am not getting any error but the values are not showing. Instead a blank row and columns are being made.
Please help me to print the values that I want to print.
As a general rule, you should keep all the logic on views.
What I'd do is zip the lists and use tuples instead.
views.py:
new_list = zip(list1, list2)
context = {
'new_list': mylist,
}
and on templates:
{% for list1_item, list2_item in new_list %}
<tr>
<td> {{ list1_item }} </td>
<td> {{ list2_item }}</td>
</tr>
{% endfor %}
I need to get the value of totale in this dictionary dynamically:
{
"periodo": "2021-12",
"corrispettivi": {
"10.00": {
"totale": -100.0,
"imposta": -9.09,
"imponibile": -90.91
},
"22.00": {
"totale": 10773.81,
"imposta": 1942.82,
"imponibile": 8830.99
}
},
"saldo": {
"totale": 10673.81,
"imposta": 1933.73,
"imponibile": 8740.08
},
"ndc": 782,
"ingressi": 782
},
in my template
{% for entrata in entrate %}
<h3>Periodo: {{ entrata.periodo }}</h3>
<table style="width:100%">
<thead>
<tr>
<th></th>
<th style="text-align:right">TOTALE</th>
<th style="text-align:right">IMPOSTA</th>
<th style="text-align:right">IMPONIBILE</th>
</tr>
</thead>
<tbody>
{% for corrispettivo in entrata.corrispettivi %}
<tr>
<th>IVA {{corrispettivo}} %</th>
<td>{{corrispettivo.totale}}</td>
<td>{{corrispettivo.imposta}}</td>
<td>{{corrispettivo.imponibile}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
but corrispettivo.totale doesn't work
I tried this guide
but I don't understand how it works.
How can I access the value of totale?
Try this:
{% for key, value in entrata.corrispettivi.items %}
<tr>
<th>IVA {{key}} %</th>
<td>{{value.totale}}</td>
<td>{{value.imposta}}</td>
<td>{{value.imponibile}}</td>
</tr>
{% endfor %}
I created a table with 3 columns.
Column one is the main test name.
Column two is the sub-test name.
Column three is the pass/fail status.
I would like to remove duplicates in column so that sub-test and status can look like they are grouped with the main test.
I tried to use the unique filter in the html below <td>{{ value['status'][0][0]|unique }}</td> but no luck there.
Basically trying to remove duplicates from column 1 of my table.
<table style="width: 100%" class="flex-container">
<tbody>
{% for key, value in testCaseStatusDict.items() %}
<tr>
<td>{{ value['status'][0][0] }}</td>
<td style="text-align: left; vertical-align: middle"><b>{{ value['testCaseName']}}</b></td>
{% for status in value['status']%}
{% if status[2] == 'FAIL' %}
<td style="background-color: red; text-align: center"><br>{{ status[2] }} <br> {{ status[3] }}</td>
{% else %}
<td style="background-color: green; text-align: center"><br>{{ status[2] }} <br> {{ status[3] }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Desired look:
Actual output:
You can try this: Check if you are already in your actual test case if yes don't print the status again else print it.
{% set status_before = [] %}
{% for key, value in testCaseStatusDict.items() %}
<tr>
{% if value['status'][0][0] in status_before %}
<td></td>
{% else %}
<td>{{ value['status'][0][0] }}</td>
{% set __ = status_before.append(value['status'][0][0]) %}
{% endif %}
...
#Edit Try it with mutable list, even if it's ugly. Looks like updating a variable inside a loop is not supported: https://github.com/pallets/jinja/issues/641
I have this problem where I can't display the contents of a dictionary into a webpage.
web_indiv[url_sequence] = {'url' : converted_url , 'name' : x.name, 'count' : web_count_current }
return render_template('video.html', web_data = web_indiv)
The web_indiv is populated using a loop, and then passed to video.html as web_data.
Sample dictionary
{1: {'url': 'http://www.drpeppersnapplegroup.com/', 'name': 'Dr. Pepper-Snapple Group', 'count': 57}, 2: {'url': 'http://www.rccolainternational.com/', 'name': 'Royal Crown Cola', 'count': 41}}
Note: It is a dictionary that contains another dictionary inside it.
This is what I already have on my html file.
{% for key1,line in web_data.items() %}
{% for key2,line_item in line.items() %}
<tr>
<td class="col-md-2">{{ line_item['url'] }}</td>
<td class="col-md-2">{{ line_item['name'] }}</td>
<td class="col-md-2">{{ line_item['count'] }}</td>
</tr>
{% endfor %}
{% endfor %}
Data won't display on the webpage.
Thank you for taking time to read my query.
If it is just a dict, you could try this:
<html>
{{web_data[url_sequence]}}
<table>
<tr>
{%for value in web_data[url_sequence].values()%}
<td class="col-md-2">{{ value }}</td>
{% endfor %}
</tr>
</table>
</html>
Note that the web_data[url_sequence] is a dictionary.
This one will have the orders (url, name and then count):
<tr>
<td class="col-md-2">{{ web_data[url_sequence].url }}</td>
<td class="col-md-2">{{ web_data[url_sequence].name }}</td>
<td class="col-md-2">{{ web_data[url_sequence].count }}</td>
</tr>
Real example:
Suppose you have the dictionary web_indiv, then you want to render it to template video.html
#app.route('/', methods=['GET'])
def root():
web_indiv = {}
url_sequence = 'test'
web_indiv[url_sequence] = {'url':'testabc','name':'hello','count': 4}
return render_template('video.html', web_data = web_indiv, url_sequence = url_sequence)
Then you can use the dict in template like this:
<tr>
<td class="col-md-2">{{ web_data[url_sequence].url }}</td>
<td class="col-md-2">{{ web_data[url_sequence].name }}</td>
<td class="col-md-2">{{ web_data[url_sequence].count }}</td>
</tr>
The html will show you:
testabc hello 4
{% for key2,line_item in web_data[url_sequence].items %}
<tr>
<td class="col-md-2">{{ line_item }}</td>
</tr>
{% endfor %}