json as table with django

Question:

I have modeled my database using models.py within my django project, one of the fields is a JSONField and I can save json data into that field without any problem. My doubt comes in how I can show that information as an html table. At the moment I have been using ListView to show that information in a template but I don’t know how to transform it into a table.

Asked By: morcilloso

||

Answers:

If you use object.json_field.items you can loop through them just like a normal dictonary / can also use .keys and .values

Example use in a table

<table>
    <thead>
        <tr>
            <th>Key</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>
        {% for k, v in object.json_field.items %}
            <tr>
                <th>{{k}}</th>
                <th>{{v}}</th>
            </tr>
        {% endfor %}
    </tbody>
</table>
Answered By: Nealium
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.