簡體   English   中英

django模板嵌套字典,按鍵獲取值

[英]django template nested dictionary, grab value by key

在django模板中,如果我在與另一個字典(嵌套字典)相關聯的上下文字典中有一個鍵,我知道如何對它進行迭代如何在django模板中的字典中迭代字典? )但我需要找到按鍵值。

{{ nested_dictionary['key'] }}

但我猜不到那個......

可以使用過濾器,但有更好的方法嗎?

是的,如果您的視圖中有嵌套字典,即:

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

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

然后,您可以在通過上下文傳遞時調用嵌套字典:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM