Home > Mobile >  Django template loop through API results to display in table
Django template loop through API results to display in table

Time:11-20

so I have some data being returned via API which I am passing into my template I am having an issue though in displaying the data as currently it is showing each char per row, I am trying to make it so in the table I have the coin name in a row with the value in the next col and so on.

Data being returned-

{"error":[],"result":{"ZGBP":"30622.0790","DASH":"0.5104491200","ADA":"2473.80445621","ZUSD":"67787.8285","KSM":"24.7142610000","CHZ":"13773.0349000000","XXLM":"6926.27220000","KNC":"0.0000000000","MATIC":"1838.9295772000","ZRX":"0.0000000000","BAL":"0.0000000000","XXDG":"17006.92601155","LINK":"144.2407000000","USDT":"60000.00000000","TRX":"923.80015900","COMP":"0.0000034600","ENJ":"257.6815000000","DOT":"0.0000000000","XLTC":"11.4923900000","SC":"0.0000000200","XZEC":"0.0000073100","SOL":"1133.3543869800","SUSHI":"172.4585500000","XXRP":"0.00000000","XETH":"14.5877343640","AAVE":"83.6218990800","ATOM":"151.26763831","XXBT":"0.0000012880","ALGO":"32063.69514500","OCEAN":"652.6077000000"}}

My template-

<table class="table table-hover table-bordered">
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Amount</th>
    </tr>
  </thead>
  <tbody>
    {% for v in api_reply %}
    <tr>
      <td>{{ v }}</td>
    </tr>
    {% endfor %}
  </tbody>
</table>

CodePudding user response:

Probably you get the string object instead of json, but if it is a json... In template use this:

<tbody>
    {% for k,v in api_reply['result'].items() %}
    <tr>
      <td>{{ k }}</td>
      <td>{{ v }}</td>
    </tr>
    {% endfor %}
 </tbody>```

CodePudding user response:

Try with: {% var api_reply=JSON.parse(api_reply) %} {% for ......

  • Related