Reputation: 329
I wrote a code using Python and trying to display all the Documents from Mongodb on a web page. However, on webpage I see the Column names, but no data. And on the command, it does print all the data. Any help is greatly appreciated.
import pymongo
from pymongo import MongoClient
import datetime
import sys
from flask import Flask, render_template, request
import werkzeug
from flask_table import Table,Col
from bson.json_util import dumps
import json
app = Flask(__name__)
try:
client = pymongo.MongoClient("XXXX")
print("Connected to Avengers MongoClient Successfully from Project
Script!!!")
except:
print("Connection to MongoClient Failed!!!")
db = client.avengers_hack_db
@app.route('/')
def Results():
try:
Project_List_Col = db.ppm_master_db_collection.find()#.limit(10)
for row in Project_List_Col:
print(row)
return render_template('Results.html',tasks=row)
except Exception as e:
return dumps({'error': str(e)})
if __name__ == '__main__':
app.run(debug = True)
The HTML (Results.html) Page is:
<html>
<body>
{% for task_id in tasks %}
<h3>{{task_id}}</h3>
{% endfor %}
</body>
</html>
Upvotes: 0
Views: 1936
Reputation: 1
Try using key,value
function of for loop and also remove that loop in the python app file from route like upper answer @Dinakar suggested
Upvotes: 0
Reputation: 329
Removed the for loop and rewrote the code as below:
@app.route('/')
def Results():
try:
Project_List_Col = db.ppm_master_db_collection.find()
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)
Documents are displayed on the HTML Page as is.
(***Will work on the formatting part. Meanwhile any pointers are greatly appreciated.)
Upvotes: 1