Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django模板嵌套字典,按键抓取值_Python_Html_Django_Templates - Fatal编程技术网

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

Python django模板嵌套字典,按键抓取值,python,html,django,templates,Python,Html,Django,Templates,在django模板中,如果上下文词典中有一个键与另一个词典(嵌套词典)关联,我知道如何对其进行重写(),但我需要按键查找值 像{{nested_dictionary['key']}} 但我猜不完全是 可以使用过滤器,但有更好的方法吗?是的,如果您的视图中有嵌套字典,即: ... dashTable = {'Key0':{'Key1':{'Key2':{'Key3':5}}}} context = {'dashTable':dashTable,} return render(request, '

在django模板中,如果上下文词典中有一个键与另一个词典(嵌套词典)关联,我知道如何对其进行重写(),但我需要按键查找值

{{nested_dictionary['key']}}

但我猜不完全是


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

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

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

context = {'dashTable':dashTable,}
return render(request, 'template.html', context)
然后,当作为上下文传递时,您将能够调用嵌套字典:

{{ dashTable.Key0.Key1.Key2.Key3 }} 

过滤器是一种方式:嗯,好的,谢谢,似乎有空间让这更容易…你总是可以写一个自定义模板标签来做这件事。不过,通常我更喜欢在我的上下文中避免这种数据结构。您尝试过{{nested_dictionary.key}}吗?对,如果在编写模板时知道密钥,则点语法可以工作。