How to run external python file and show data in front end of django in the form of table
Hi Guys I want to run my external py scipt and the output will be on json I want to show the data in form of rows and table in front end, the data is dynamic regression testing data. how to I show that using django
Here I tried to give an answer as per I understand your question.
dummy JSON
data take (you can take you JSON
data which received from file)
json_response_from_file= [
{
"title": "HP Pavilion 15-DK1056WM",
"price": 1099,
"brand": "HP Pavilion",
"category": "laptops",
"thumbnail": "https://i.dummyjson.com/data/products/10/thumbnail.jpeg",
"images": [
"https://i.dummyjson.com/data/products/10/1.jpg",
"https://i.dummyjson.com/data/products/10/2.jpg",
"https://i.dummyjson.com/data/products/10/3.jpg",
"https://i.dummyjson.com/data/products/10/thumbnail.jpeg"
},
{
"title": "Redmi I-10",
"price": 1100,
"brand": "Redmi",
"category": "mobile",
"thumbnail": "https://i.dummyjson.com/data/products/10/thumbnail.jpeg",
"images": [
"https://i.dummyjson.com/data/products/10/1.jpg",
"https://i.dummyjson.com/data/products/10/2.jpg",
"https://i.dummyjson.com/data/products/10/3.jpg",
"https://i.dummyjson.com/data/products/10/thumbnail.jpeg"
},
]
Django views.py
import json
def Oneview(request):
data=file_json_response
context = {'data':json.loads(json_response_from_file)}
return render(request,'index.html',context)
HTML Code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" >
</head>
<body>
<table class="table">
<thead>
<tr>
<th scope="col">No.</th>
<th scope="col">Thumbnail</th>
<th scope="col">Name</th>
<th scope="col">Price</th>
<th scope="col">Brand</th>
<th scope="col">Category</th>
<th scope="col">All-Images</th>
</tr>
</thead>
<tbody>
{% for i in data %}
<tr>
<th >{{forloop.counter}}</th>
<td>{{i.thumbnail}}</td>
<td>{{i.title}}</td>
<td>{{i.price}}</td>
<td>{{i.brand}}</td>
<td>{{i.category}}</td>
<td>
{% for j in i.images %}
<div>{{j}}</div>
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" ></script>
</body>
</html>