将日志从Mongodb渲染到烧瓶路由

时间:2019-12-20 02:29:54

标签: python mongodb logging flask

client = MongoClient("mongodb:xxxx")
db = client.databasename
collection = db.logs    

@app.route('/log')
def Results():
    try:
        loggings = db.collection.find()
        return render_template('index.html', loggings=loggings)
    except Exception as e:
        return dumps({'error': str(e)})

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

这是我的app.py和'index.html'代码,我的代码如下

<!doctype html>
<html>
    <body>
        {% for message in logs %}
            <h3>{{message}}</h3>
        {%endfor%}
    </body>
</html>

运行代码时,它在localhost:xxxx / log路由上不显示任何内容。

我可以知道为什么吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

如果在

中使用loggings=
render_template('index.html', loggings=loggings)

然后您还必须在模板中使用loggings

{% for message in loggings %}

但是您使用{% ... in logs %}