Reputation: 59
I have the following JSON file
[{
"ID": 1,
"Name": "John Smith",
"IDNumber": "7606015012088"
},
{
"ID": 2,
"Name": "Molly Malone",
"IDNumber": "8606125033087"
}]
Which I want to display it in table format.I have parsed the json file using json.load(filename)
I have tried something like:
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>
But it is not printing the anything?
Upvotes: 3
Views: 11575
Reputation: 21
from json2html import *
def read_data(request): with open('data.json', 'r') as f: input_data = json.load(f)
done = json2html.convert(json=input_data)
with open(".//templates//dataview.html",'w') as f:
f.writelines(done)
return render(request, 'dataview.html')
Upvotes: 2
Reputation: 423
The only problem I see is that you have used {% if data %}
. Instead use {% if d %}
. As you sent data
as d
.
Upvotes: 3