【问题标题】:Dictionary inside a dictionary in django templatedjango模板中字典内的字典
【发布时间】:2018-03-06 11:38:24
【问题描述】:

我有字典,其中有:

a = {'card1' :{'name':'abc','amount':'145820'},'card2':{'name':'dba','amount':'258963'}}

现在,我想在 Django 模板中打印 card1.amount。我该怎么做?

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    Django 模板语言允许您进行多个查找,例如{{ a.b.c.d }}。在您的示例中,您似乎希望在模板中使用 {{ a.card1.amount }}

    【讨论】:

    • 我尝试使用 {{ a.card1.amount }} ,但我没有得到任何东西
    • 如果这不起作用,您需要显示您的视图。目前我只能猜测你的模板上下文是什么。
    • 在我看来,我将上面的 json 发送到 django 模板。在模板中,我只想要 card1 和 card2 中的金额
    • 除非你展示你的观点,否则我无能为力。您最初的问题没有在任何地方提到 JSON。
    • a = {'card1' :{'name':'abc','amount':'145820'},'card2':{'name':'dba','amount': '258963'}} 返回渲染(request,templatename.html,{'a':a})
    猜你喜欢
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2018-10-22
    • 2011-12-22
    • 2019-10-22
    • 2013-05-06
    相关资源
    最近更新 更多