简体   繁体   English

Django-将数组参数传递给视图

[英]Django - Passing array parameter to a view

Are you able to return an array through to a django webpage from the python Views.py file? 您是否可以从python Views.py文件将数组返回到django网页? There may be a way, but I can't figure out how to do it. 可能有办法,但是我不知道该怎么做。 Now, if I put the array into a dictionary, and return that, I can access the array, which is fine for what I need to do, but I would like to know if there is a way to just return the array and access it on the django template. 现在,如果我将数组放入字典中并返回,则可以访问该数组,这对我需要做的事情很好,但是我想知道是否有一种方法可以只返回数组并访问它在Django模板上。 Thanks: 谢谢:

I can access the following on the template with catarray.0.name. 我可以使用catarray.0.name在模板上访问以下内容。

catarray = [{"name": "Henry", "type": "Tiger", "Awesomeness": "Very"}, {"name": "Bob", "type": "Siamese", "Awesomeness": "Cool"}]
returnobject={'catarray': catarray}
return render(request, "home.html", returnobject)

I cannot access the following and was wondering if it's even possible. 我无法访问以下内容,并且想知道是否有可能。

catarray = [{"name": "Henry", "type": "Tiger", "Awesomeness": "Very"}, {"name": "Bob", "type": "Siamese", "Awesomeness": "Cool"}]
return render(request, "home.html", catarray)

Thanks! 谢谢!

I'm not sure why you would want to do this. 我不确定您为什么要这样做。 The reason that you always pass a dict to render is that each item has a key, which is the name by which you access it in the template. 您始终传递字典以进行渲染的原因是,每个项目都有一个键,这是您在模板中访问它的名称。 If you passed a list, you wouldn't have a key; 如果您通过了列表,则不会有密钥。 how would you reference any of the values? 您将如何引用任何值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM