【问题标题】:Django HTML can't read the values of the dictDjango HTML 无法读取字典的值
【发布时间】: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 条件。

【问题讨论】:

标签: python html django dictionary render


【解决方案1】:

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

{{ myDict.key }}

由于 django 使用点符号作为模板的字典。

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 2022-08-18
    • 2019-06-18
    • 2013-11-12
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    相关资源
    最近更新 更多