我运行一个python瓶应用程序,它需要一个用户名和密码。我想检查Mysql数据库上存在的用户名。如果是的话,我想回“的用户名存在”的网页上。
下面是我的烧瓶代码:
from flask import Flask,render_template,request,redirect
from flask_mysqldb import MySQL
import yaml
app = Flask(__name__)
db = yaml.load(open('db.yaml'))
app.config['MYSQL_HOST'] = db['mysql_host']
app.config['MYSQL_USER'] = db['mysql_user']
app.config['MYSQL_PASSWORD'] = db['mysql_password']
app.config['MYSQL_DB'] = db['mysql_db']
mysql =MySQL(app)
@app.route('/', methods=['GET','POST'])
def index():
if request.method == 'POST' and request.method=='GET':
#fetch form data
userdetails = request.form
name = userdetails['name']
email = userdetails['email']
cur = mysql.connection.cursor()
new_value= cur.execute("SELECT (name,email) FROM users where name = %s and email=%s",'name','email')
if new_value> 0:
return "the username exists"
else:
return "SUCCESS!,Successfully entered into the Database"
return render_template('index.html')
当我运行的应用程序烧瓶,我的网页犯规返回任何东西。
行,所以我不能发表评论,因为我没有足够的声誉然而,因为这是当用户点击一个按钮提交表单获取表单数据的request.method
需要被POST
。布赖恩·德里斯科尔说,request.method
不能同时POST
和GET
同时,它像是在说:
if bob.age == 15 and bob.age == 50
鲍勃不能既是15岁到50岁在同一时间,以同样的方式request.method
不能在同一时间POST
和GET
。所以你想要做的是
if request.method == 'POST':
userdetails = request.form
name = userdetails['name']
email = userdetails['email']
cur = mysql.connection.cursor()
new_value= cur.execute("SELECT (name,email) FROM users where name = %s and email=%s",'name','email')
if new_value> 0:
return "the username exists"
else:
return "SUCCESS!,Successfully entered into the Database"
有关POST
,GET
和其他要求的详细信息,你可以参考文章here