我已經在我的 django 專案中使用 models.py 對我的資料庫進行了建模,其中一個欄位是 JSONField,我可以毫無問題地將 json 資料保存到該欄位中。我的疑問在于如何將這些資訊顯示為 html 表格。目前我一直在使用 ListView 在模板中顯示該資訊,但我不知道如何將其轉換為表格。
uj5u.com熱心網友回復:
如果你使用object.json_field.items,你可以像普通字典一樣回圈遍歷它們/也可以使用.keys和.values
表格中的示例使用
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/517419.html
