Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

API returning an array inside of an object how to display contents- Django

So I am requesting spot price from cex API, it returns something like this
{"data":[{"amount: "67000.0", "base": "BTC", "currency": "USD"}]} of course there is more than one returned so I want to loop over it.

In my views.py I am passing it into my context 'price': price. Then inside of my .html file I have a list with a for loop for example:

<ul>
  {% for x in price %}
    <li>{{ x }}</li>
  {% endfor %}
</ul>

Then when I open my .html page I get

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • data
  • But what I’d like to be able to do is make a table where I have three columns to show the amount, base and currency. I am unsure on how to extract this data individualy?

    >Solution :

    You can enumerate over the .values of price, and then enumerate over the dictionaries in that list with:

    <table>
    {% for vs in price.values %}
        {% for v in vs %}
            <tr><td>{{ v.amount }}</td><td>{{ v.base }}</td><td>{{ v.currency }}</td></tr>
        {% endfor %}
    {% endfor %}
    </table>
    Add a comment

    Leave a Reply

    Keep Up to Date with the Most Important News

    By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

    Discover more from Dev solutions

    Subscribe now to keep reading and get access to the full archive.

    Continue reading