简体   繁体   English

Django 模板访问字典键

[英]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.目前我正在将一个包含多个键的字典从我的视图传递给 django 模板,第一个键包含一个包含所有想要的键的列表,我想要一个嵌套循环在第一个键内循环,这样我就可以在想要的内部访问键。

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:在这种情况下,我只想访问 dict['name1'] 和 dict['name5'],在 Python 中我可以执行以下操作:

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我想知道我是否可以对 html 文件中的 django 模板执行相同的操作

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" : [],
}

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

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