【发布时间】:2021-04-08 07:04:30
【问题描述】:
当我将一个简单的字典 (myDict = {"key": "value"}) 通过渲染传递给 html 时,它会产生一个错误,因为 html 无法读取字典的值..
{{ myDict["key"] }} <!--- prompts an error-->
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '['key']' from 'myDict['key']'
我为什么不直接传值呢?因为我的问题不是关于这个特定的例子。我正在寻找一种更有效的方式来使用,以防我想传递更大的字典并避免在每一行写下 for 循环和 if 条件。
【问题讨论】:
-
Django 模板甚至对字典也使用点符号。
标签: python html django dictionary render