我正在运行一个python Flask应用程序,它需要用户名和密码。我想检查Mysql数据库中是否存在用户名。如果是这样,我想在网页上返回“username exists”。在
以下是我的烧瓶代码:
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')
当我运行flask应用程序时,我的网页没有返回任何内容。在
好的,所以我不能评论,因为我没有足够的声誉来获取表单数据,},这是一个类似的说法:
request.method
必须是POST
,因为那是用户单击按钮提交表单的时候。正如Brian Driscoll所说,request.method
不能同时是POST
和{鲍勃不能同时是15岁和50岁,同样地,}。
所以你想做的是
^{pr2}$request.method
不能同时是POST
和{有关
POST
、GET
和其他请求的更多信息,请参阅文章here相关问题 更多 >
编程相关推荐