我有以下的JSON文件显示JSON作为表在Django
[{
"ID": 1,
"Name": "John Smith",
"IDNumber": "7606015012088"
},
{
"ID": 2,
"Name": "Molly Malone",
"IDNumber": "8606125033087"
}]
,我想在表format.I显示它已经解析使用json.load(文件名)
我JSON文件曾尝试类似:
Views.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")
demo.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%}' – SatanDmytro