python flask和htmljavascript从mysql返回响应并显示

2024-06-16 11:19:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python flask从mysql数据库获取响应JSON,并将其显示在html页面中。在

@app.route("/augment_withdate1", methods=['GET', 'POST'])
def augment_withdate1():
   query = session.get('query', None)
   date = session.get('date', None)
   db = pymysql.connect(host="localhost",  # your host
                     user="root",       # username
                     passwd="",     # password
                     db="pythontesting")   # name of the database

   # Create a Cursor object to execute queries.
   cur = db.cursor()

   # Select data from table using SQL query.
   cur.execute("""SELECT augment,intent FROM response WHERE query= %s 
                AND DATE(date)= %s """, query, date)

   rows = cur.fetchall()
   row_headers=[x[0] for x in cur.description] #this will extract row 
   #headers

   json_data=[]
   for result in rows:
       json_data.append(dict(zip(row_headers,result)))
   return json.dumps(json_data)

我的html代码是:

^{pr2}$

我想知道: 1如何打印响应而不是显示为警报? 2有没有一种方法可以一次从数据库中获取所有数据,并在用户单击时将每列显示为一个按钮?在

谢谢


Tags: none数据库jsondbdatagetdatesession