How to get only the value from the SQL query output without parentheses

352 views Asked by At

Here is my code in the flask app

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)

which returns this

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

But I want to show like this DHK, KHL,......

How can I get this and use it in HTML template

UPDATE

Here is the index.html file

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

1

There are 1 answers

1
cubitouch On

Can you try with this? I think you need to loop arround this value you pass to the template:

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