django 模板嵌套字典,按键抓取值

jay*_*jay 5 html python django templates

在 django 模板中,如果我在上下文字典中有一个键与另一个字典(嵌套字典)相关联,我知道如何遍历它(如何在 django 模板中的字典中遍历字典?)但我需要找到键值。

喜欢 {{ nested_dictionary['key'] }}

但我猜不完全是这样...

可以使用过滤器,但有更好的方法吗?

Jos*_*osh 1

是的,如果您的视图中有嵌套字典,即:

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

context = {'dashTable':dashTable,}
return render(request, 'template.html', context)
Run Code Online (Sandbox Code Playgroud)

然后,当作为上下文传递时,您将能够调用嵌套字典:

{{ dashTable.Key0.Key1.Key2.Key3 }} 
Run Code Online (Sandbox Code Playgroud)