0
votes

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

1
"I only want to access dict['name1'] and dict['name5']". Then you should only pass to the template what it needs instead of a complex structure.Klaus D.
@KlausD. The above is just an example, name2,name3,name4 will be used in other occasions, but will not be called in the same way as name1 and name5LurkerDoge
And why don't you just create a more simple and therefore more template friendly data structure? Data processing should happen in Python, not in the template.Klaus D.
@KlausD. So in the template, I want to have multiple select choices, with each select tag containing multiple option tags, so with the above example, I have a list containing different types of select that I want the users to choose from, and each type of select have a corresponding list of options that the type of select has. That's why I thought it would be easier if there's a way to do it in the templateLurkerDoge

1 Answers

0
votes

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