如何将格式化的数据从MongoDB发布到HTML页面?

时间:2019-08-25 15:31:37

标签: python html mongodb flask

我正在使用HTML将格式正确的数据发布到网页上。我用Flask和Mongodb作为我的数据库在python中编写了代码。

System.Net.HttpClient

但是,数据以JSON格式打印。我希望将其格式化为表格格式。

这是我的HTML代码:

@app.route('/')
def Results():
    try:
        Project_List_Col = db.ppm_master_db_collection.find({}, 
   {"_id":0})
    return render_template('Results.html',tasks=Project_List_Col)
except Exception as e:
    return dumps({'error': str(e)})

if __name__ == '__main__':  
   app.run(debug = True)

当前输出类似于:

<html>
   <body>
     {% for task_id in tasks %}
        <h3>{{task_id}}</h3>
     {% endfor %}
   </body>
</html>

但是,我需要这样:

[{u'total': 9942806, u'_id': {u'd': 1, u'sid': u'c1'}},
 {u'total': 10173832, u'_id': {u'd': 1, u'sid': u'c2'}},
 {u'total': 9567489, u'_id': {u'd': 1, u'sid': u'c3'}}]

我需要有关如何获得可读性更好的UI输出的帮助。

1 个答案:

答案 0 :(得分:0)

以下HTML代码解决了该问题:

<html>
   <body>
         {% for task_id in tasks %}
            <h3>{{task_id}}:{{tasks[task_id]}}</h3>
         {% endfor %}
   </body>
</html>

在进一步研究中,我基本上了解Task_id是字典。