亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Flask 中使用 DropDownMenu 獲取 MongoDB 中的所

如何在 Flask 中使用 DropDownMenu 獲取 MongoDB 中的所

偶然的你 2022-06-14 16:13:26
我的目標是使用 Bootstrap 的 dropdownMenu,其中菜單中的每個項目都獲取我的 MongoDB 的 IdObject。原因是我希望將這些 IdObjects 放在一個列表中,以便獲取存儲在該集合中的所有數據。因此,這是我的代碼:HTML<div class="dropdown-menu" aria-labelledby="dropdownMenu2">    {% for row in rows %}       <button class="dropdown-item" href="./get_object?_id={{row['_id']}}" type="button">{{row['_id']}}</button>    {% endfor %}</div>[email protected]("/get_object", methods=['POST', 'GET'])def get_object():    cursor = object_collection.find({})    for document in cursor:        row = document['_id']        return render_template("get_object.html", rows=row)不知何故,我沒有得到我想要的。我在 python 文件和 HTML 中有一些錯誤。我這樣做的方式好嗎?  File "?/application/app.py", line 52, in get_object    return render_template("get_object.html", rows=row)  File ?/application/templates/get_object.html", line 18, in block "content"    {% for row in rows %}
查看完整描述

1 回答

?
犯罪嫌疑人X

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>



查看完整回答
反對 回復 2022-06-14
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號