Python code to print a dictionary in Jinja2 templating - python

I'm trying to print a dictionary that looks like for e.g.
groups = {'place': ABC, 'name': XYZ , 'IP': ['1.1.1.1','2.2.2.2']}
in a table in Jinja2 templating format:
I've a word document that looks like this for now:
I know this isn't how it is supposed to look and I'm stuck as I'm a complete beginner in Jinja2. Could someone help me out here? I want to know how I can modify my table in the word document to get the required result.
PS: The code on the word document currently is:
{% tr for key,value in groups.items() %} {{key}} {% for elem in value %}{{ elem }}{% if value|length > 0 and value.index(elem) != value|length-1 %} {%endif%}{% endfor %} {%tr endfor %}
Essentially my dict should look like:
Place
Name
IP
ABC
XYZ
1.1.1.1, 2.2.2.2
The table in the image attached is what I tried for now, but it throws errors

you can try something like this.
<table>
<thead>
<tr>
{% for key in groups.keys() %}
<th>{{ key|title }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for key in groups.keys() %}
<td>{{ groups[key]}}</td>
{% endfor %}
</tr>
</tbody>
</table>

Related

Can't print the proper values of Python dictionary in HTML

I'm trying to parse a Python dictionary into HTML and display it as a table.
I have an SQL database that I draw data from using Python, then parse it into my page like so:
tusername = query_db("SELECT t_username FROM teacher")
return render_template('teachers.html.',username=tusername)
I found this piece of code here, which prints the data:
<table>
{% for t in table %}
<tr>
{% for i in t %}
<td>{{ i }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
This is what it prints:
t_username
t_username
t_username
t_username
t_username
This is my data:
[{'t_username': '1'}, {'t_username': 'admin'}, {'t_username': 'dfsdsfdf'}, {'t_username': 'NewGuy'}, {'t_username': 'test'}]
I want it to print the proper names rather than "t_username". I've not been able to find a solution and I've only just begun working with Python, so I'm not very familiar with it. Anyone able to help?
Provided that t is a dictionary, {% for i in t %} iterates over it's keys (i.e. a single t_username key). Instead just output the value by the key:
{% for t in table %}
<tr>
<td>{{ t.t_username }}</td>
</tr>
{% endfor %}

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>

How to traverse a list which contains many dicts in Django template?

Now,I have a dict in view and want to present in HTML in the form of table:
and I pass it into context:
context['cities']=cities
then,render it:
render(request,'index.html',context=context)
In 'index.html':
<table>
<tr>
<th>name</th>
<th>population</th>
<th>country</th>
</tr>
{% for city in cities%}
<tr>
<td>{{city.name}}</td>
<td>{{city.population}}</td>
<td>{{city.country}}</td>
</tr>
{% endfor %}
</table>
Unfortunately,it seems that Django template can't support this kind of traverse,so the result can't show successfully as I expected.
"td" tags are empty.
did you try something like that:
render(request,'index.html',context={
"cities": cities
})
Also, you can try into your template:
{% for city in cities %}
<tr>
{% for key, value in city %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Where key will be name, population, etc

Color coding cells in a table based on the cell value using Jinja templates

I have a simple flask app and need to display a table of values, with the cell backgrounds colour coded based on the cell value according to thresholds. I'm generating the table content as follows:
{% block dashboard_table2 %}
<table>
{% for row in data %}
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endblock %}
I tried wrapping the values in style tags like this in Python but it didn't work:
if int(value) <= 10:
value = '<p style="background-color:Red">' + value + '</p>'
I'm guessing the CSS for the page is overriding the style attribute. I also tried just setting the text color attribute instead of background-color but no dice. Any suggestions on a good way to do this? I'd like to have a concise way to specify threshold values that aren't hard-coded in the templates.
The easiest way would be to put this display logic in your template:
<table>
{% for row in data %}
<tr>
{% for item in row %}
{% if item <= 10 %}
<td class="under-limit">{{ item }}</td>
{% else %}
<td>{{ item }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
Then, in your CSS you can use:
.under-limit { background-color: red; }
<table>
{% for row in row %}
{% if item <= 10 %}
<tr style ="background-color: red">
<td> {{ item }} </td>
</tr>
{% else %}
<tr>
<td> {{ item }} </td>
</tr>
{% endif %}
{% endfor %}
</table>
This works for me.

Categories

Resources