gpt4 book ai didi

python - 在 Django 中将 JSON 显示为表

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:34 25 4
gpt4 key购买 nike

我有以下 JSON 文件

[{
"ID": 1,
"Name": "John Smith",
"IDNumber": "7606015012088"
},
{
"ID": 2,
"Name": "Molly Malone",
"IDNumber": "8606125033087"
}]

我想以表格格式显示它。我已经使用 json.load(filename) 解析了 json 文件

我试过类似的东西:

View .py

import json

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.template.loader import render_to_string

# Create your views here.

with open('/home/kunal.jamdade/Desktop/PyCharmProjects/demo.json') as d:
data = json.load(d)


def load_json_table_format(request):
print(data)
html = render_to_string()
return HttpResponse({'d':data}, 'demoApp/demo.html', content_type="application/html")
#return JsonResponse(data, safe=False,content_type="application/html")
#return render(request, 'demoApp/demo.html', {'d': data}, content_type="application/html")

演示.html

<body>
{% if data %}
<table>
{% for k in d %}
{% for item_1, item_2 in k.items %}
<tr>
<td>{{ item_1 }}</td>
<td>{{ item_2 }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
{% endif %}
</body>

但它没有打印任何东西?

最佳答案

我看到的唯一问题是您使用了 {% if data %}。而是使用 {% if d %}。当您将 data 作为 d 发送时。

关于python - 在 Django 中将 JSON 显示为表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46661319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com