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

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

Related

Python code to print a dictionary in Jinja2 templating

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>

Populate html table using multiple jinja dictionary

Greeting, I want to populate a HTML table using multiple jinja dictionary reference but doing it like my code below will create duplication instead because of the for loop inside a for loop, how can I fix this, is there any front-end way, or I have to change my function in view.py?
Any help is much appreciated, thanks
view.py
for employee_id in get_employee_id_list:
get_employee_data = WorkOrder.objects.filter(assign_to=employee_id)
total_task = get_employee_data.__len__()
total_employee_task_list.append(total_task)
context = {
"employee_list": get_employee_id_list,
"total_employee_task_list": total_employee_task_list,
}
return render(request, 'report.html', context)
HTML
<tbody>
{% for employee in employee_list %}
{% for total in total_employee_task_list %}
<tr>
<td>{{ employee.official_name }}</td>
<td>{{ total }}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>

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

Categories

Resources