Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django template nested dictionary, grab value by key

In a django template, if I have a key within the context dictionary assocoated with another dictionary (a nested dictionary), I know how to itterate over it (how to iterate through dictionary in a dictionary in django template?) but I need to find a value by key.

like {{ nested_dictionary['key'] }}

But I'm guessing not exactly that...

Filters could be used, but is there a better way?

like image 487
jayjay Avatar asked Nov 11 '22 19:11

jayjay


1 Answers

Yes, if you have a nested dictionary in your view i.e.:

...
dashTable = {'Key0':{'Key1':{'Key2':{'Key3':5}}}}

context = {'dashTable':dashTable,}
return render(request, 'template.html', context)

Then you would be able to call your nested dictionary when passed through as context with:

{{ dashTable.Key0.Key1.Key2.Key3 }} 
like image 161
Josh Avatar answered Nov 14 '22 23:11

Josh