简体   繁体   中英

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? 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. Thanks:

I can access the following on the template with 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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