0

这是我在烧瓶应用程序中的代码

from flask import Flask, render_template
from flask_mysqldb import MySQL
import MySQLdb.cursors

app = Flask(__name__)

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASWPRD'] = ''
app.config['MYSQL_DB'] = 'customer'

mysql = MySQL(app)

@app.route('/')
def index():
     cur = mysql.connection.cursor()
     cur.execute("SELECT City FROM customer")
     data = cur.fetchall()
     cur.close()
   return render_template ("index.html", name=data)
if __name__ == "__main__":
   app.run(debug=True)

返回这个

Hello World (('DHK',), ('KHL',), ('RAJ',), ('COM',))

但是我想展示这样的DHK,KHL,......

我怎样才能得到它并在 HTML 模板中使用它

更新

这是 index.html 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask App</title>
</head>
<body>
    <head>Hello World</head>
    
    <h1>{{ name }}</h1>
</body>
</html>

4

1 回答 1

0

你可以试试这个吗?我认为您需要循环传递给模板的这个值:

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask App</title>
  </head>

  <body>

    <head>Hello World</head>

    {% for message in name %}
      {% for item in message %}
        {{ item }},
      {% endfor %}
    {% endfor %}
  </body>

</html>
于 2020-05-18T17:20:40.753 回答