1 回答

TA貢獻2080條經驗 獲得超4個贊
你只想要一個清單?,F在你有循環return內。for相反,只需附加到列表并立即使用整個列表調用模板:
@app.route("/get_object", methods=['POST', 'GET'])
def get_object():
rows = [] # define an empty list
cursor = object_collection.find({},{ "_id": 1 })
for document in cursor:
rows.append(document['_id']) # <- append to the list
return render_template("get_object.html", rows=rows) # Use the whole list in output
另請注意,.find({},{ _id: 1 })僅投影結果 中的字段而不是整個對象。因此,當您只需要這些值以便不通過網絡發送不必要的數據時,這很有用。_id_id
在您的模板中,這現在只是一個values列表,因此沒有_id屬性。只需使用值:
<button class="dropdown-item" href="./get_object?_id={{row}}" type="button">{{row}}</button>
添加回答
舉報