Multicolumn table in django template - python

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.

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.

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

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.

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