我有这个注册页面,当我提交表单时出现此错误:并非所有参数都在字符串格式化期间转换
class RegistrationForm(Form):
email = StringField('Email address')
password = PasswordField('password')
name = StringField('Name')
@app.route('/register/', methods=['GET', 'POST'])
def register():
try:
form = RegistrationForm(request.form)
if request.method == 'POST':
email = form.email.data
password = sha256_crypt.encrypt((str(form.password.data)))
con = connection()
cur=con.cursor()
x = cur.execute("SELECT * FROM user WHERE username = (%s)",(thwart(email)))
if int(x) > 0:
return render_template('register.html',form=form)
else:
cur.execute("INSERT INTO user (username,password,name) VALUES (%s,%s,%s);",(thwart(email),thwart(password),thwart(name),))
con.commit()
cur.close()
con.close()
return redirect(url_for('dashboard'))
return render_template('register.html', form=form)
except Exception as e:
return str(e)
最佳答案
x = cur.execute("SELECT * FROM user WHERE username = (%s)",(thwart(email)))
execute
的第二个参数应该是一个元组,你缺少一个 ,
:
cur.execute("SELECT * FROM user WHERE username = (%s)",(thwart(email),))
我猜你也不需要 %s
周围的 ()
但这取决于你的表的实际外观。
进一步说明:
('str')
将求值为字符串 'str'
,而不是包含它的元组。
为了创建一个元组,您必须包含一个逗号:('str',)
。
关于python - 错误 :not all arguments converted during string formatting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474576/