【问题标题】:How to get value from dict in a loop in a django template如何在 django 模板的循环中从 dict 获取值
【发布时间】:2021-12-28 18:37:22
【问题描述】:

我需要动态获取此字典中totale的值:

{
    "periodo": "2021-12",
    "corrispettivi": {
        "10.00": {
            "totale": -100.0,
            "imposta": -9.09,
            "imponibile": -90.91
        },
        "22.00": {
            "totale": 10773.81,
            "imposta": 1942.82,
            "imponibile": 8830.99
        }
    },
    "saldo": {
        "totale": 10673.81,
        "imposta": 1933.73,
        "imponibile": 8740.08
    },
    "ndc": 782,
    "ingressi": 782
},

在我的模板中

{% for entrata in entrate %}
<h3>Periodo: {{ entrata.periodo }}</h3>
<table style="width:100%">
    <thead>
        <tr>
            <th></th>
            <th style="text-align:right">TOTALE</th>
            <th style="text-align:right">IMPOSTA</th>
            <th style="text-align:right">IMPONIBILE</th>
        </tr>
    </thead>

    <tbody>
        {% for corrispettivo in entrata.corrispettivi %}
        <tr>
            <th>IVA {{corrispettivo}} %</th>
            <td>{{corrispettivo.totale}}</td>
            <td>{{corrispettivo.imposta}}</td>
            <td>{{corrispettivo.imponibile}}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endfor %}

但是corrispettivo.totale 不起作用

我试过this guide

但我不明白它是如何工作的。

如何获取totale 的值?

【问题讨论】:

  • 你也可以使用自定义模板标签..

标签: python django


【解决方案1】:

试试这个:

{% for key, value in entrata.corrispettivi.items %}
   <tr>
       <th>IVA {{key}} %</th>
       <td>{{value.totale}}</td>
       <td>{{value.imposta}}</td>
       <td>{{value.imponibile}}</td>
   </tr>
{% endfor %}

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2021-07-27
    • 2011-08-30
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多