返回多个变量Django的渲染
我在 views.pys.py
中定义 home
请求,
db=client.inventory_data
def home(request):
collection_data_1 = db['orders']
mydata = list(collection_data.find())
return render(request,'home.html',{'mydata': mydata})
上面的功能正常工作,但是当我尝试返回另一个列表时, 不工作。
def home(request):
collection_data_1 = db['orders']
collection_data_2 = db['product']
mydata = list(collection_data_1.find())
product_data = list(collection_data_2.find())
return render(request,'home.html',{'mydata': mydata},{'product_data':product_data})
我想返回两个列表,我们该如何实现?寻找善良的帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需将两个字典组合到一个:
{'mydata':mydata,'product_data':product_data}
当您分别通过两个字典时它不起作用的原因是因为
渲染接受
上下文
(字典)为第三个参数,content_type
(a String)为第四参数,因此,当您通过两个字典中传递时,您将词典以content_type
传递。如果有帮助,这是您最初用注释的变量名称所拥有的:
这就是您现在拥有的:
You can simply combine the two dictionaries into one:
{'mydata': mydata, 'product_data': product_data}
The reason that it didn't work when you passed in the two dictionaries separately is because
render
acceptscontext
(a dictionary) as the third argument andcontent_type
(a string) as the fourth argument, so when you passed in two dictionaries, you were passing in a dictionary as thecontent_type
.If it helps, here's what you originally had with the variable names annotated:
And here's what you have now: