Django HTML 无法读取字典的值

Django HTML can't read the values of the dict

当我通过渲染将一个简单的字典 (myDict = {"key": "value"}) 传递给 html 时,它会产生一个错误,因为 html 无法读取字典的值 ..

{{ myDict["key"] }} <!--- prompts an error-->

django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '['key']' from 'myDict['key']'

为什么我没有直接传值呢?因为我的问题不是关于这个特定的例子。我正在寻找一种更有效的使用方法,以防我想传递更大的命令并避免写下 for 循环和每一行的条件。

您可以在模板中试试这个:

{{ myDict.key }}

由于 django 使用点符号表示模板的字典。