3

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?

1
  • 3
    Try to rewrite {% if data %} to {% if d %} Commented Oct 10, 2017 at 7:56

2 Answers 2

3

The only problem I see is that you have used {% if data %}. Instead use {% if d %}. As you sent data as d.

Sign up to request clarification or add additional context in comments.

3 Comments

It is giving error saying "Type Error" "__init__() got multiple values for keyword argument 'content_type'"
Ok, use render(request, 'demoApp/demo.html', {'d': data} instead of HttpResponse
thanks. I did the same way. Instead of content_type="application/html" is used content_type="text/html".
2

pip install json2html # Use this Library

In Views.py

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')

This is working fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.