How to read list in django template - django

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>

Related

Populating Table Based on Drop Down Selection

Hi I'm trying to create a table that will show values based on the selected name. For instance if a drop down existed and a value was chosen, the rest of the fields would show the data associated with that chosen value.
<div class="main-items">
<h2>Database</h2>
<div class="card-items">
<table class="main-items-table">
<tr>
<td class="menu-box-tab-items-identifiers">Name:</td>
{% for d in database %}
<td class="menu-box-tab-items" href="/cusip/{{d.company_name}}"><span>{{d.name}}</span></td>
{% endfor %}
</tr>
<tr>
<td class="menu-box-tab-items-identifiers">Item:</td>
{% for d in database %}
<td class="menu-box-tab-items"><span>{{d.item}}</span></td>
{% endfor %}
</tr>
<tr>
<td class="menu-box-tab-items-identifiers">Method of Shipment:</td>
{% for d in database %}
<td class="menu-box-tab-items" href="#6"><span>{{d.method_of_shipment}}</span></td>
{% endfor %}
</tr>
<tr>
<td class="menu-box-tab-items-identifiers">Days:</td>
{% for d in database %}
<td class="menu-box-tab-items" href="#6"><span>{{d.days}}</span></td>
{% endfor %}
</tr>
<tr>
<td class="menu-box-tab-items-identifiers">Location:</td>
{% for d in database %}
<td class="menu-box-tab-items"></span>{{d.location}}</td>
{% endfor %}
</tr>
<tr>
<td class="menu-box-tab-items-identifiers">Country:</td>
{% for d in database %}
<td class="menu-box-tab-items" href="#6"><span>{{d.country}}</span></td>
{% endfor %}
</tr>
</tbody>
</table>
</div>
</div>
Basically, if a drop down menu existed for all the "names", I would want all the other td's to show the data associated with that name. If anyone can help it would be greatly appreciated.
Your options are:
To require the user to submit the form once the first option has been selected and to then refresh the template with related options in the other fields. You would need some code in your forms.py file that can take the provided option and filter the other querysets accordingly.
Use javascript and an AJAX request to dynamically update the other field options once the first field has been selected, here is some useful information:
Django Ajax Jquery Call

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

Html table rendering

My Django view is returning a list of dictionaries. This goes to html table through template rendering. Below is my template code,
My list of dictionary looks like below,
Results :
[{ 'name':'x','age': 20}, {'name': 'y','age': 25 }]
<table class="table table-striped" border="1" class="dataframe">
<thead>
<tr style="text-align: center;">
{% for k, v in results.0.items %}
<th>{{ k }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for x in results %}
<tr style="text-align: center;">
{% for y in x %}
<td> {{ x.y }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Expected output:
name age
x 20
y 25
But the output is blank.
Please let me know if there is anything wrong with my HTML table template.
You can use items properly in your inner loop just like the outer loop
<div>
<table class="table table-striped" border="1" class="dataframe">
<thead>
<tr style="text-align: center;">
{% for k, v in results.0.items %}
<th>{{ k }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for x in results %}
<tr style="text-align: center;">
{% for i,j in x.items %}
<td> {{ j }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>

Adding rows breaks datatable in Django

I have a datatable in Django that functions fine when hardcoded, but when I add my Django template tags it breaks. The inspect on the page says: Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined in jquery.datatables.min.js
This only happens when I have more than one user in the table, or try to add a column in the table with django. Since I will be using multiple datatables in my project, I need to figure out what I'm doing wrong. The datatable JS code I'm using comes from a template, and I'm not sure if the error is in the template code or in my Django template code. So please excuse the long code blocks.
employees.html (django template):
<div class="card-body collapse in">
<div class="card-block card-dashboard">
<button id="addRow" class="btn btn-primary mb-2 js-create-employee"><i class="ft-plus"></i> Add New Employee</button>
<table class="table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th>Name</th>
<th>Username</th>
<th>Roles</th>
<th>Email</th>
<th>Mobile Contact</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for profile in user_profile_list %}
<tr>
{% if not profile.user.is_superuser %}
<td>{{ profile.user.get_full_name }}</td>
<td>{{ profile.user.username }}</td>
<td>
{% for g in profile.user.groups.all %}
<div class="tag tag-default">{{ g.name|split:'_'|title }}</div>
{% endfor %}
</td>
<td>{{ profile.user.email }}</td>
<td>{{ profile.mobile_phone }}</td>
<td><i class="fa fa-pencil"></i> <a href="#" alt="Assign"><i class="fa fa-link"></i><a/> <a href="#" alt="delete"><i class="fa fa-times"></i><a/></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th>Name</th>
<th>Username</th>
<th>Roles</th>
<th>Email</th>
<th>Mobile Contact</th>
<th>Actions</th>
</tr>
</tfoot>
</table>
Datatable instantiation:
$(document).ready(function() {
/****************************************
* js of zero configuration *
****************************************/
$('.zero-configuration').DataTable();
});
Jquery.datatables.min.js and dataTables.bootstrap4.min.js are also used, but those come stock from bootstrap 4. I'm not going to add them here unless needed, and they are minified anyway.
This issues occurs only when data is not available for the table or tags.
You have conditions in template using django templatestag So the number of every <td> element in your table that is a child of a <tr> element doesn't match the number of <th> elements that are a child of the element.
Please make sure you have same number of <td> tag in <tbody> tag.
EDIT:
Maybe {% if not profile.user.is_superuser %} this condition creating this issue. If user is superuser then no <td> tag will create that will not match same number of <th> in <thead>

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

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>