user8427236 user8427236 - 6 months ago 21
HTML Question

Table appears empty without any value (Django)

My data are like this :

13.2610136284,20.8676059554,11.6376896892,0.51203356592,167,20154.4815106,
395,766,56,55047.3477967,-26818.1766435,3072.97868784,12.5402166873,-1443.
74556748,-4.0


I need to display it as a table in html
This is the function used in python to transform the data suitable for a table

import string
from datetime import datetime, date

def ratio_fil():

ratioFil = open('sergRatiosOut20170426','r')
rat = ratioFil.read().split(',')
ratioFil.close()

return rat


Rat is similar to this :

['13.2610136284', '20.8676059554', '11.6376896892', '0.51203356592',
'167', '20154.4815106', '395', '766', '56', '55047.3477967',
'-26818.1766435', '3072.97868784', '12.5402166873', '-1443.74556748',
'-4.0\r\n']


I need to put it in a table, So i create a data_table tags :



<tr><td>X</td>
<td>{{data_table.0|floatformat:1}} %</td></tr>

<tr><td>y</td>
<td>{{data_table.1|floatformat:1}} %</td></tr>

<tr><td>y1 </td>
<td>{{data_table.2|floatformat:1}} </td></tr>

<tr><td>y2</td>
<td>{{data_table.3|floatformat:1}} </td></tr>




In Html :

<div> {% with rat as data_table %}
{% include 'tags/data_table.html' %}
{% endwith %}
</div>


The table appears empty, i think it should be a method to import rat ? Please i need your help

Answer Source

You can use builtin for loop template tag to loop through and render the data in HTML table

<table>
  <tr>
    <th>Column Name</th>
  </tr>
  {% for data in data_table %}
  <td>
     <td>{{ data }} </td>
  </td>
  {% endfor %}
</table>

Remember to return the data from your view

def ratio_fil():

 ratioFil = open('sergRatiosOut20170426','r')
 rat = ratioFil.read().split(',')
 ratioFil.close()

 return render(request, 'template_name.html', {'data_table': rat})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download