如何将响应类型作为参数发送到python中的html文件?

时间:2019-12-23 09:00:10

标签: python json response pyodbc flask-restful

我正在python flask中创建api。我将SQL Server用于数据库,并通过SELECT查询成功获取了数据,并使用flask的jsonify返回了json格式的数据。 myapi.py文件为:

from flask import Flask, request, render_template, url_for, redirect, flash
from flask_restplus import Api, Resource
from flask import jsonify
import pyodbc

flask_app = Flask(__name__)
api = Api(app=flask_app)

# creating connection Object which will contain SQL Server Connection
conn = pyodbc.connect('Driver={SQL Server};'
                  'Server=TABRIZIYAN;'
                  'Database=market_DB;'
                  'Trusted_Connection=yes;')

class ProductList(Resource):
    def get(self):  # will be used to fetch all record from tbl_product 
    try:
        cursor =conn.cursor()
        cursor.execute("SELECT * FROM Tbl_product")
        columns = [column[0] for column in cursor.description]
        results = []
        for row in cursor.fetchall():
            results.append(dict(zip(columns, row)))
        resp = jsonify(results)
        resp.status_code = 200
        return resp
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        conn.close()

api.add_resource(ProductList , '/product')

if __name__ == '__main__':
flask_app.run(debug=True)

当我在http://localhost:5000/product上运行它时,我看到了:

[
     {
        "P_ID": 1,
        "title": "product1",
        "count": 100,
        "price": 1000,
        "active": true
    },
    {
        "P_ID": 2,
        "title": "Product2",
        "count": 12,
        "price": 2500,
        "active": false
    },
]

现在,我想将resp作为参数发送到product.html文件中的app.py。我的app.py文件是:

from flask import Flask,render_template,url_for, request, redirect, flash, jsonify
import pyodbc
import requests

app = Flask(__name__)

@app.route('/product')
def product():
    info = requests.get('http://localhost:5000/product') 
    return render_template('product.html', info=info)

if __name__=='__main__':
app.run(debug=True, port="8080")

我的product.html是:

{% block body %}
<body>
<h1>Products List</h1>

<table  style="text-align: center" width="15%" border="3px" bgcolor="#FFE4E1">
    <tr><td><a href='/add'>Add New Product</a></td></tr>
</table>
<br><br>
<table style="text-align: center" width="50%" align="left" border= "3px solid black" border- 
 collapse= collapse bgcolor="#E0FFFF">
    <tr>
        <th> ID </th>
        <th> Title </th>
        <th> Count </th>
        <th> Price </th>
        <th> Active </th>
        <th> Actions </th>
   </tr>
    {% for product in info %}
    <tr>
        <td> {{product[0]}} </td>
        <td> {{product[1]}} </td>
        <td> {{product[2]}} </td>
        <td> {{product[3]}} </td>
        <td> {{product[4]}} </td>
        <td> <a href='/edit/{{product[0]}}'>Edit</a> 
        <a href='/delete/{{product[0]}}' onclick="return confirm('Are You Sure To Delete?')">Delete</a> </td>

    </tr>
   {% endfor %}
</table>
</body>
{% endblock %}

我想使用rest api在resp中将product.html字典中的值显示为表格,但我不知道如何将info作为参数发送给product.html因为info类型为Response,所以我的代码无法正常运行。你能帮我吗?

1 个答案:

答案 0 :(得分:1)

使用响应关键字发送多个值

return Response({"res":resp},status=HTTP_200_OK)