我试图在表单中输入值并从数据库中获取值,但我做不到。我正在使用名为travel的mysql数据库,table也被命名为travel。我在下面附加我的python代码和模板 我的错误是:werkzeug.exceptions.BadRequestKeyError werkzeug.exceptions.HTTPException.wrap..newcls:400错误请求:KeyError:'stay'
Python代码:
from flask import Flask, render_template, request, redirect,flash,url_for,session
from flask_mysqldb import MySQL
import datetime
import time
app = Flask(__name__)
app.config['MYSQL_HOST'] = "127.1.1.0"
app.config['MYSQL_USER'] = "root"
app.config['MYSQL_PASSWORD'] = "****"
app.config['MYSQL_DB'] ="travel"
app.secret_key="gsabo"
mysql = MySQL(app)
@app.route('/')
@app.route('/questions',methods=['GET', 'POST'])
def questions():
if request.method == "POST" or "GET":
userDetails= request.form
cur = mysql.connection.cursor()
stay=userDetails['stay']
Budget=userDetails['Budget']
print(Passengers,Weather,Transport,stay,Interests)
cur.execute("select * from travel where stay=%s,Budget=%s",(stay,Budget,))
mysql.connection.commit()
userDetails=cur.fetchone()
Location=userDetails[3]
Budget=userDetails[4]
return render_template('questions.html',Budget=Budget,Location=Location)
return render_template('questions.html')
if __name__ == '__main__':
app.run(debug=True)
HTML模板:
^{pr2}$
有了这个代码块,大概可以用
value = "Hotel"
或value = "Tent"
向/提问,但代码中只说明value = "stay"
。你需要对每一个案子进行解释相关问题 更多 >
编程相关推荐