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