Home > OS >  json as table with django
json as table with django

Time:10-19

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.

CodePudding user response:

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>
  • Related