如何在Django模板中以有序形式打印cvs内容

时间:2018-06-25 14:43:57

标签: python django

这是我的Django应用... 它将以django模板打印csv内容,但不是以有组织的形式打印。(请参阅输出)。

views.py

from django.shortcuts import render
import pandas as pd

def index(req):
    df = pd.read_csv("Users\Documents\data\myproject\datanalysis\PastHires.csv")
    f=df.head()
    return render(req,'index.html', {'content':f}) 

<html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{{content}}
</body>
</html>

OUTPUT
Years Experience Employed? Previous employers Level of Education \ 0 10 Y 4 BS 1 0 N 0 BS 2 7 N 6 BS 3 2 Y 1 MS 4 20 N 2 PhD Top-tier school Interned Hired 0 N N Y 1 Y Y Y 2 N N N 3 Y N Y 4 Y N N

但是我想在我的Django模板中这样

Years Experience Employed?  Previous employers Level of Education  \
0                10         Y                   4                 BS
1                 0         N                   0                 BS
2                 7         N                   6                 BS
3                 2         Y                   1                 MS
4                20         N                   2                PhD

  Top-tier school Interned Hired
0               N        N     Y
1               Y        Y     Y
2               N        N     N
3               Y        N     Y
4               Y        N     N

1 个答案:

答案 0 :(得分:0)

您需要将内容放在表格中,看看示例here

<body>
    <table>
      <tr>
        <th>Years of Experience</th>
        <th>Employment Status</th>
        <th>Previous Employers</th>
        <th>Level of Education</th>
      </tr>
      {% for data in f %}
      <tr>
        <td>{{data.experience}}</td>
        <td>{{data.employment}}</td>
        <td>{{data.previous}}</td>
        <td>{{data.education}}</td>
      </tr>
      {% endfor %}
    </table>
</body>