将对象列表传递给 HTML 页面进行循环并将数据显示为对象列表
Pass list of object to HTML page for looping and display data as list of object
我正在尝试将对象列表传递给 HTML 以循环呈现从 MongoDB
获取的数据
尝试过的方法:
创建了一个全局变量作为对象列表。从 MongoDB 中获取数据并将数据设置为全局变量
并尝试直接获取它,但无法访问意味着没有错误,也没有数据。
在索引定义期间将对象的获取列表作为渲染传递(请求)
x=mycol.find()
downloadData=list(x)
return render(request, 'index.html',downloadData)
错误:
File "C:\xxx\views.py", line 30, in index
return render(request, 'index.html',downloadData)
AttributeError: 'list' object has no attribute 'dict'
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 %}
谁能指出我在这两种方法中做错了什么?
如果你能指出正确的link会有所帮助
我认为你应该像下面那样将 downloadData
作为参数传递
return render(request, 'index.html', {'downloadData': downloadData})
您应该像这样在上下文中将数据作为字典传递,
return render(request, 'index.html',{'downloadData'=downloadData})
在HTML中,您可以这样访问它,
{% for d in downloadData %}
<div class="row content">
<h2>d</h2>
</div>
{% endfor %}
我正在尝试将对象列表传递给 HTML 以循环呈现从 MongoDB
获取的数据尝试过的方法:
创建了一个全局变量作为对象列表。从 MongoDB 中获取数据并将数据设置为全局变量
并尝试直接获取它,但无法访问意味着没有错误,也没有数据。
在索引定义期间将对象的获取列表作为渲染传递(请求)
x=mycol.find() downloadData=list(x) return render(request, 'index.html',downloadData)
错误:
File "C:\xxx\views.py", line 30, in index return render(request, 'index.html',downloadData)
AttributeError: 'list' object has no attribute 'dict'
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 %}
谁能指出我在这两种方法中做错了什么?
如果你能指出正确的link会有所帮助
我认为你应该像下面那样将 downloadData
作为参数传递
return render(request, 'index.html', {'downloadData': downloadData})
您应该像这样在上下文中将数据作为字典传递,
return render(request, 'index.html',{'downloadData'=downloadData})
在HTML中,您可以这样访问它,
{% for d in downloadData %}
<div class="row content">
<h2>d</h2>
</div>
{% endfor %}