我正在构建一个python烧瓶-Mysql应用程序。我正在使用AWS cloud9构建它。但是,当我运行代码时,我会得到MYSQL_HOST键错误。我在下面附加代码。是因为安装错误还是代码错误?
from flask import Flask, request, render_template
from flask_mysqldb import MySQL
application = Flask(__name__)
application.config['MYSQL_HOST'] = 'localhost'
application.config['MYSQL_USER'] = 'nfhfjfn'
application.config['MYSQL_PASSWORD'] = 'fsfc'
application.config['MYSQL_DB'] = 'fsvf'
application.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(application)
# mysql.init_app(application)
application = Flask(__name__)
@application.route("/")
def hello():
cursor = mysql.connect().cursor()
cursor.execute("SELECT * from LANGUAGES;")
mysql.connection.commit()
languages = cursor.fetchall()
languages = [list(l) for l in languages]
return render_template('index.html', languages=languages)
if __name__ == "__main__":
application.run(host='0.0.0.0',port=8080, debug=True)
`
发布于 2021-07-21 05:09:54
你给application = Flask(__name__) 打了两次电话。因此,第二次要覆盖第一个application。它应该是:
from flask import Flask, request, render_template
from flask_mysqldb import MySQL
application = Flask(__name__)
application.config['MYSQL_HOST'] = 'localhost'
application.config['MYSQL_USER'] = 'nfhfjfn'
application.config['MYSQL_PASSWORD'] = 'fsfc'
application.config['MYSQL_DB'] = 'fsvf'
application.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(application)
# mysql.init_app(application)
#application = Flask(__name__) <--- remove that
@application.route("/")
def hello():
cursor = mysql.connect().cursor()
cursor.execute("SELECT * from LANGUAGES;")
mysql.connection.commit()
languages = cursor.fetchall()
languages = [list(l) for l in languages]
return render_template('index.html', languages=languages)
if __name__ == "__main__":
application.run(host='0.0.0.0',port=8080, debug=True)https://stackoverflow.com/questions/68464024
复制相似问题