Flask,并非所有参数都在字符串格式化期间转换
Flask, not all arguments converted during string formatting
尝试为我的应用程序创建一个注册页面。我正在使用 Flask 框架和来自 pythonanywhere.com 的 MySQL 数据库。
@app.route('/register/', methods=["GET","POST"])
def register_page():
try:
form = RegistrationForm(request.form)
if request.method == "POST" and form.validate():
email = form.email.data
password = sha256_crypt.encrypt((str(form.password.data)))
c, conn = connection()
x = c.execute("SELECT * FROM users WHERE email = (%s)",
(email))
if int(x) > 0:
flash("That email adress is already in use.")
return render_template('register.html', form=form)
else:
c.execute("INSERT INTO users (email, password) VALUES (%s, %s)",
(thwart(email),thwart(password)))
conn.commit()
flash("Thanks for registering!")
c.close()
conn.close()
gc.collect()
session['logged_in'] = True
session['email'] = email
return redirect(url_for('dashboard'))
return render_template("sign-up.html", form=form)
except Exception as e:
return(str(e))}
在 运行 我得到 Error:not 在字符串格式化期间转换的所有参数。
如何解决?可能是这个声明中的问题?
c.execute("INSERT INTO users (email, password) VALUES (%s, %s)",
(thwart(email),thwart(password)))
只是将我之前的评论转换为答案,因为它似乎是正确的解决方案:-)
问题来自不同的线路。你有这个:
x = c.execute("SELECT * FROM users WHERE email = (%s)",
(email))
这与您想象的不同。将 email
放在方括号中没有任何作用,因此该行实际上等效于传入字符列表中该变量中的任何字符的每个字符。相反,如果您这样做:
x = c.execute("SELECT * FROM users WHERE email = (%s)",
(email,))
...那么您将传递一个包含一项的元组,email
,它应该会更好。
尝试为我的应用程序创建一个注册页面。我正在使用 Flask 框架和来自 pythonanywhere.com 的 MySQL 数据库。
@app.route('/register/', methods=["GET","POST"])
def register_page():
try:
form = RegistrationForm(request.form)
if request.method == "POST" and form.validate():
email = form.email.data
password = sha256_crypt.encrypt((str(form.password.data)))
c, conn = connection()
x = c.execute("SELECT * FROM users WHERE email = (%s)",
(email))
if int(x) > 0:
flash("That email adress is already in use.")
return render_template('register.html', form=form)
else:
c.execute("INSERT INTO users (email, password) VALUES (%s, %s)",
(thwart(email),thwart(password)))
conn.commit()
flash("Thanks for registering!")
c.close()
conn.close()
gc.collect()
session['logged_in'] = True
session['email'] = email
return redirect(url_for('dashboard'))
return render_template("sign-up.html", form=form)
except Exception as e:
return(str(e))}
在 运行 我得到 Error:not 在字符串格式化期间转换的所有参数。 如何解决?可能是这个声明中的问题?
c.execute("INSERT INTO users (email, password) VALUES (%s, %s)",
(thwart(email),thwart(password)))
只是将我之前的评论转换为答案,因为它似乎是正确的解决方案:-)
问题来自不同的线路。你有这个:
x = c.execute("SELECT * FROM users WHERE email = (%s)",
(email))
这与您想象的不同。将 email
放在方括号中没有任何作用,因此该行实际上等效于传入字符列表中该变量中的任何字符的每个字符。相反,如果您这样做:
x = c.execute("SELECT * FROM users WHERE email = (%s)",
(email,))
...那么您将传递一个包含一项的元组,email
,它应该会更好。