大家好,我想运行我的外部py scipt,输出将在json上我想在前端以行和表的形式显示数据,数据是动态回归测试数据。如何使用django显示
carvr3hs1#
在这里我试着给予一个答案,因为我理解你的问题。
JSON
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" }, ]
import json def Oneview(request): data=file_json_response context = {'data':json.loads(json_response_from_file)} return render(request,'index.html',context)
<!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>
1条答案
按热度按时间carvr3hs1#
在这里我试着给予一个答案,因为我理解你的问题。
虚拟
JSON
数据获取(您可以获取从文件接收的JSON
数据)Django views.py
HTML代码