我正在嘗試將物件串列傳遞給 HTML 以回圈呈現從 MongoDB 獲取的資料
嘗試的方法:
創建了一個全域變數作為物件串列。從 MongoDB 中獲取資料并將資料設定為全域變數
并嘗試直接獲取它,但這不可訪問意味著沒有錯誤,也沒有資料。
在索引(請求)的定義期間傳遞物件的獲取串列作為渲染
x=mycol.find() downloadData=list(x) return render(request, 'index.html',downloadData)錯誤:
檔案“C:\xxx\views.py”,第 30 行,索引回傳渲染(請求,'index.html',downloadData)
AttributeError:“串列”物件沒有屬性“字典”
HTML 代碼:
{% for d in downloadData %} <div class="row content"> <h2>I Love Food</h2> <p>Food is my passion. Lorem ipsum dolor sit amet, consectetur adipiscing elit, </p> </div> {% endfor %}
誰能指出我在這兩種方法中做錯了什么?
如果您能指出正確的鏈接將有所幫助
uj5u.com熱心網友回復:
您應該在這樣的背景關系中將資料作為字典傳遞,
return render(request, 'index.html',{'downloadData'=downloadData})
在 HTML 中,你可以像這樣訪問它,
{% for d in downloadData %}
<div class="row content">
<h2>d</h2>
</div>
{% endfor %}
uj5u.com熱心網友回復:
我認為你應該通過downloadData如下的引數
return render(request, 'index.html', {'downloadData': downloadData})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/469370.html
標籤:Python python-3.x 列表
上一篇:我正在創建一個通過用戶輸入呼叫函式的文本冒險游戲。在start()中輸入1會呼叫start_observe,但2會呼叫start()。為什么?
