У меня есть список данных вопросов в базе данных Mongodb.Я хочу отображать вопрос за вопросом на HTML-странице.Это мой код Flask
from flask import Flask, render_template, request, url_for
from pymongo import MongoClient
from bson.json_util import dumps
import json
client = MongoClient('localhost:27017')
db = client.girish
app = Flask(__name__)
@app.route("/questions", methods = ['GET'])
def questions():
try:
names = db.questions.find({},{"ques":1,"options":1,"_id":0,"quesid":1,"ans":1}).limit(10)
return render_template('index.html', names=names)
#return dumps(names)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/answers", methods = ['POST'])
def answers():
try:
data = json.loads(request.data)
quesid = data['quesid']
yourans = data['yourans']
if quesid and yourans:
status = db.myanswers.insert_one({
"quesid" : quesid,
"yourans" : yourans
})
return dumps({'message' : 'SUCCESS'})
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/myanswers", methods = ['GET'])
def myanswers():
try:
answers = db.myanswers.find({},{"quesid":1,"yourans":1})
return dumps(answers)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/check/<id>", methods = ['GET','POST'])
def check(id):
try:
doc=db.questions.find({'quesid':float(id)})
return dumps(doc)
except Exception as e:
return dumps({'error': str(e)})
if __name__ == '__main__':
app.run(port='5002',debug = True)
Каковы возможные способы отображения данных, представленных в monodb, на странице html?Я новичок в веб-разработке на Python.Пожалуйста, помогите мне в подробном процессе.
Это мой код внедрения mogodb с python
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["girish"]
mycol = mydb["questions"]
mylist = [
{
"ques" : "what is test?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 1,
"ans" : 1
},
{
"ques" : "what is test2?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 2,
"ans" : 1
},
{
"ques" : "what is test3?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 3,
"ans" : 1
},
{
"ques" : "what is test4?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 4,
"ans" : 1
},
{
"ques" : "what is test5?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 5,
"ans" : 1
},
]
x = mycol.insert_many(mylist)
Как я могу создать HTML-страницу и как связаться с моей базой данных?