Home > database >  Django looping through items not showing
Django looping through items not showing

Time:11-24

Hi I am looping through items being passed through the context but nothing is showing.

This is the data I have:

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

It is passed in my context like this:

def kraken(request):
    """ A view to return kraken page """
    context = {
        'api_reply': api_reply,
    }
    return render(request, 'home/kraken.html', context)

And inside my template I have this:

{% for k, v in api_reply.items %}
<tr>
  <td>{{ k }}</td>
  <td>{{ v }}</td>
</tr>
{% endfor %}

I have no errors showing but it is not displaying, any help would be great thank you.

CodePudding user response:

The items are stored in the result subelement, you thus should enumerate over api_reply.result.items, not api_reply.items:

{% for k, v in api_reply.result.items %}
<tr>
  <td>{{ k }}</td>
  <td>{{ v }}</td>
</tr>
{% endfor %}

Furthermore you need to convert the JSON blob into Python objects, for example with:

import json

def kraken(request):
    """ A view to return kraken page """
    context = {
        'api_reply': json.loads(api_reply),
    }
    return render(request, 'home/kraken.html', context)
  • Related