简体   繁体   中英

I Have an array of dictionaries and I want to use them in django. How do I do that?

I have a array of dictionaries like:

[{'name': 'X','class':'A'},{'name':'Y','class':'B'}]

I Want the Html to show:

Name:X, Class:A
Name:Y, Class:B

I'm working on Django project Please Help

I Have tried to pass the array and separate out the two dictionaries but I'm not able to access the keys inside the dictionaries

Python

params=my_base2.fetch_allreq(email)
data=[]
for i in params:
    dic={'name': i[0],
         'email': i[1],
         'item': i[2],
         'room': i[3]}
    data.append(dic)
print(data)
content={
 'Requests':data
}
return render(request,'dashboard.html',content )

HTML

  {% for lists in Requests %}
   {{lists}}

  {% endfor %}

In the render method, you need to pass content as the context named variable:

return render(request, 'dashboard.html', context=content)

Then your code should work.

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