for loop django template logic - how can I do this? - django

I am trying to output 2 items for each row. I have 4 items coming from db.
<table>
<tr>
{% for item in items %}
<td>
{{item.name}},{{item.size}}
</td>
{% endfor %}
</tr>
</table>
this is giving me
name1, 23m^2 | name2,20m^2 | name3,15m^2 | name4,10m^2
but i need
name1, 23m^2 | name2,20m^2
name3,15m^2 | name4,10m^2
each row being contained in separate <tr>. I am stuck how to break the loop and assign new row..

Just switch the <tr> and forloop, and also use forloop.counter and divisibleby
Something like this:
{% if items %}
<tr>
{% for item in items %}
<td>{{item.name}},{{item.size}}</td>
{% if forloop.counter|divisibleby:2 %}
</tr>
<tr>
{% endif %}
{% endfor %}
</tr>
{% endif %}

You forgot to close <td> tag.
<table>
<tr>
{% for item in items %}
<td>
{{item.name}},{{item.size}}
</td> <!-- here -->
{% endfor %}
</tr>
</table>

Related

How to read list in django template

Could someone shed a light to help me to solve this nested list problem in Django template.
I come across a list as such
[['Summary 1 CODE018'], [['Directory/File Name', 'Result']], [[['dir/var1/file1.txt', 'pass'], ['dir/var2/file2.txt', 'pass'], ['dir/var1/file3', 'pass']]], [[['null']]]]
How do I loop the above list so that I can get a table such as the first index is the table title, index1 is the table title and the rest (except when is it null) is the table body? Please take note that the last item could be [[['null']]] or [[['null', 'Info on directory CODEA18']]] which should be skip when null is detected.
Expected table:
Directory/File Name Result
dir/var1/file1.txt pass
dir/var2/file2.txt pass
dir/var3/file3.txt pass
My faulty code as below
<table id="myTable">
{% for list in summary %}
<tr>
<td> {{ list.0 }} </td>
<tbody>
<td> {{ list.0.0 }} </td>
<td> {{ list.0.0.0 }} </td>
</tbody>
</tr>
</table>
You list have 4 items. In that 2nd is Table heading and 3rd is table row.
We need to run 2 loops, one for thead and one for tbody
<table border>
<thead>
<tr>
{% for i in a[1][0] %}
<th>{{i}}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for i in a[2][0] %}
<tr>
{% for j in i%}
<td>{{j}}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>

Django HTML template table rendering

I want to render a table look like this:
<table>
<tr>
<td>some data</td>
<th>special one</th>
<td>some data</td>
...
<td>some data</td>
</tr>
...
</table>
There is a solution that can render all in the same tag.
<table>
{% for rowval in results %}
<tr>
{% for val in rowval %}
<td>{{val}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
But in my case, there would be a th at the second place for every row of the data, if there is a record.
There is another solution that not as good as the answer below as it keeps partial table, td and tr in the view.
Is there a way to implement this feature?
There are some variables available inside a django template for loop, one of them is named forloop.counter which gives you the current iteration of the loop. You can use this variable to render something differently on the second loop
<table>
{% for rowval in results %}
<tr>
{% for val in rowval %}
{% if forloop.counter == 2 %}
<th>{{ val }}</th>
{% else %}
<td>{{ val }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>

Django template tags - arrange result of queryset

I am trying to format my results in a table.
<tr>
<td>A6</td>
<td>A6 title</td>
<td>
{% for record in a6titles %}
titles: {{ record }}
{% endfor %}
</td>
</tr>
The data displays in a single row. I would like each result item to be displayed in its own row. I am sure this is very simple; however, I am very new to this.
# You need to put your tr tag in for loop
{% for record in a6titles %}
<tr>
<td>A6</td>
<td>A6 title</td>
<td>titles: {{ record }}</td>
</tr>
{% endfor %}

django template table rows do not collapse

I can not work-out what I am doing wrong with collapsing certain rows of my table.
Can you advice how can I correct it so I can collapse rows marked in red ?
I guess I have placed collapse tag in wrong place or have not used again ? However can I use table in table ?
my template:
{% extends "base.html" %}
{% block content %}
<h3>{% for project in projects %}{{ project.project_name }}{% endfor %}</h3>
<table class="table table-striped table table-bordered table table-hover table table-condensed">
<tr>
<td></td>
<!-- row with zone names -->
{% for z in zones %}
<td style="width:40px" align="center">{{ z.zone_name }}</td>
{% endfor %}
</tr>
<!-- rows with stage names -->
{% for trstage in trstages %}
<tr>
<td style="width:40px"><a data-toggle="collapse" href="#collapse{{ trstage }}">{{ trstage.stage_name }}</a></td>
<!-- row with stage value -->
{% for stage in trstage.stages %}
{% for zone in zones|dictsort:"zone_name" %}
{% if zone == stage.zone %}
<td style="width:40px" align="center">{{ stage.substage_sum.sum }}</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
<div id="collapse{{ trstage }}" class="panel-collapse collapse">
<!-- rows with substages names -->
{% for trsubstage in trstage.related_trsubstages %}
<tr>
<td>{{ trsubstage.substage_name }}</td>
<!-- row with substage value -->
{% for substage in trsubstage.substages_related %}
{% for zone in zones %}
{% if zone == substage.stage.zone %}
<td style="width:40px" align="center">{{ substage.substage_value }}</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
</div>
{% endfor %}
</table>
{% endblock %}
Below is link to my current template view:
Try adding the id of your trstage to the href and id of the divs. I did something similar in my project and it worked. I hope it works for you too. Like this,
<div id="{{ trstage.id.value }}" class="panel-collapse collapse">
and
<a data-toggle="collapse" href="#{{ trstage.id.value }}">{{ trstage.stage_name }}

Syntax error in Django template

I'm getting Invalid block tag: 'else'
The code is rather simple:
<tr>
<td>...</td>
</tr>
{% ifequal var1 "string" %}
{% for i in range5 %}
{% with v.i as an %}
{% if an %}
<tr>
<td>...</td>
</tr>
{% else %}
<tr>
<td style="background-color:#A8A8A8"> </td>
</tr>
{% endif %}
{% endwith %}
{% endfor %}
<tr>
<td style="background-color:#A8A8A8"> </td>
</tr>
ERROR HERE --> {% else %}
{% for i in range5 %}
{% with .. %}
{% if .. %}
<tr>
<td>></td>
</tr>
{% else %}
<tr>
<td style="background-color:#A8A8A8"> </td>
</tr>
{% endif %}
{% endwith %}
{% endfor %}
{% endifequal %}
And else tag must be within an if tag in django templates. The last else tag does not fall into any if tag since you ended the if statement with endif.