Flask,并不是在字符串格式化时转换的所有参数 [英] Flask, not all arguments converted during string formatting
问题描述
@ app.route('/ register /',methods = [GET,POST])
)def register_page():
try:
form = RegistrationForm(request.form)
如果request.method ==POST和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(该电子邮件地址已被使用)
返回render_template('register.html',form = form)
else:
c.execute(INSERT INTO用户(电子邮件,密码)VALUES(%s,%s),
(thwart(电子邮件),thwart(密码)))
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( (b)b
除了作为e:
return(str(e))}
在运行时,我得到错误:不是在字符串格式化时转换的所有参数。
如何解决?可能是这个声明中的问题?
$ b
c.execute(INSERT INTO users(email,password)VALUES(%s,%s),
(email),thwart(password)))
,因为它似乎是正确的解决方案: - )
问题来自不同的路线。你有这样的:
$ p code $ x = c.execute(SELECT * FROM users WHERE email =(%s),
$(email))
这不符合您的想法。把电子邮件
放在括号里什么也不做,所以这行实际上等同于在字符列表中传递变量中的每个字符。如果你这样做:
$ b $ $ p code $ x c.execute(SELECT * FROM users WHERE email =(%s),
(email,))
...然后你会传入一个元组包含一个项目,电子邮件
,它应该更好。
Try to create a register page for my app. I am using Flask framework and MySQL db from pythonanywhere.com.
@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))}
On running I get the Error:not all arguments converted during string formatting. How to fix it? May be the problem in this statement?
c.execute("INSERT INTO users (email, password) VALUES (%s, %s)",
(thwart(email),thwart(password)))
Just converting my earlier comment to an answer, as it seemed to be the right solution :-)
The problem is coming from a different line. You have this:
x = c.execute("SELECT * FROM users WHERE email = (%s)",
(email))
This doesn't do what you might think it does. Putting email
in brackets does nothing, so the line is actually equivalent to passing in each character of whatever's in that variable in a list of characters. If instead you do this:
x = c.execute("SELECT * FROM users WHERE email = (%s)",
(email,))
...then you'll be passing in a tuple containing one item, email
, and it should work better.
这篇关于Flask,并不是在字符串格式化时转换的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!