如何运行外部python文件并在django前端以表格形式显示数据

n3ipq98p  于 2022-12-21  发布在  Python
关注(0)|答案(1)|浏览(144)

大家好,我想运行我的外部py scipt,输出将在json上我想在前端以行和表的形式显示数据,数据是动态回归测试数据。如何使用django显示

carvr3hs

carvr3hs1#

在这里我试着给予一个答案,因为我理解你的问题。

虚拟JSON数据获取(您可以获取从文件接收的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"
      },
    
      ]

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代码

<!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>

相关问题