嘿伙计我正在运行这段代码,当我在“/ d”上启动它时,我希望它打印出整个数据库,此时它只打印一行并更改为下一个Object如果我刷新页面,你能帮忙吗?
from flask import Flask,render_template
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client.fliDb
app = Flask(__name__)
games = db.games
g= games.find()
@app.route('/d', methods = ["GET","POST"])
def content():
for game in g:
str(game)
return str(game)
答案 0 :(得分:1)
每次请求页面时都需要创建一个新的PyMongo Cursor,而不是使用全局游标。此外,您需要完全迭代游标,而不是在第一次迭代后从循环内部返回。这样做:
@app.route('/d', methods = ["GET","POST"])
def content():
g = games.find()
return '\n'.join([str(game) for game in g)])