Rendering logs from Mongodb into flask route

老子叫甜甜 提交于 2020-01-06 04:46:07

问题


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)

This is my code for app.py and for 'index.html', my code looks like

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

When I run the code, it does not display anything on the localhost:xxxx/log route.

May I know why? Thanks!


回答1:


If you use loggings= in

render_template('index.html', loggings=loggings)

then you have to use loggings also in template

{% for message in loggings %}

but you use {% ... in logs %}



来源:https://stackoverflow.com/questions/59419198/rendering-logs-from-mongodb-into-flask-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!