简体   繁体   中英

Django template access dictionary key

Currently I am passing a dictionary with multiple keys from my views to django template, and the first key contains a list with all the wanted keys, and I want to have a nested loop to loop inside the first key so I can access inside the wanted keys.

dict = {
"list1" = ["name1", "name5"]
"name1" = []
"name2" = []
"name3" = []
"name5" = []
}

In this case, I only want to access dict['name1'] and dict['name5'], in Python I can do something like:

for name in dict['list1']:
  for l in dict[name]:
    do things

I want to know if I can do the same with django template inside html file

first you will get syntax error you should not use = you should use : in dictionary and add , after each key value pair

dict = {
"list1" : ["name1", "name5"],
"name1" : [],
"name2" : [],
"name3" : [],
"name5" : [],
}

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