表显示为空而没有任何值(Django)

时间:2017-08-16 17:03:36

标签: html django

我的数据是这样的:

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

我需要在html中将其显示为表格 这是python中用于转换适合表的数据的函数

import string
from datetime import datetime, date

def ratio_fil():

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

 return rat

大鼠与此类似:

['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']

我需要把它放在一个表中,所以我创建了一个data_table标签:

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

在Html中:

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

表格显示为空,我认为它应该是导入鼠标的方法吗?我需要你的帮助

1 个答案:

答案 0 :(得分:0)

您可以使用内置for loop模板标记循环并在HTML表格中呈现数据

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

请记住从视图中返回数据

def ratio_fil():

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

 return render(request, 'template_name.html', {'data_table': rat})