Home > Mobile >  How to get value from dict in a loop in a django template
How to get value from dict in a loop in a django template

Time:12-29

I need to get the value of totale in this dictionary dinamically:

{
    "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
},

in my template

{% 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 %}

but corrispettivo.totale doesn't work

i tried this guide

but I don't understand how it works

how can I access the value of totale?

CodePudding user response:

Try this:

{% 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 %}
  • Related