我正在尝试注册一个用户。如果输入的用户名已经存在于数据库中,我在阻止用户注册方面有问题。
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
if request.method == "GET":
return render_template("register.html")
elif request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 400)
# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 400)
# Ensure password confirmation was submitted
elif not request.form.get("password-confirmation"):
return apology("must provide password confirmation", 400)
# Check if the password confirmation field matches the password field
elif request.form.get("password-confirmation") != request.form.get("password"):
return apology("Your passwords didn't match", 400)
# Query database for that username
row = db.execute("SELECT * FROM users WHERE username = ?", request.form.get("username"))
# Database
database = db.execute("SELECT * FROM users")
# Ensure username is not already in the database
if row in database:
return apology("username already exists", 400)
# Insert username and password into the database AND Hash user's password
insert = db.execute("INSERT INTO users (username,hash) VALUES(?,?)" , request.form.get("username"), generate_password_hash(request.form.get("password")))
# Remember which user has logged in
session["user_id"] = insert
# Redirect user to home page
return redirect("/")没有得到“用户名已经存在”,而是得到“内部服务器错误”.。
有人能说明一下为什么会是这样吗?
发布于 2021-07-25 18:12:52
您的SELECT语法错了。应该是rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
发布于 2021-07-27 11:29:23
这个if row in database:永远不会是真的。
row是一个列表,所以它试图在database中找到一个列表。database本身就是一个列表,但不包含任何列表,只包含字典。
如果row不是空列表,程序将知道用户中是否已经有用户名。暗示,暗示。
https://stackoverflow.com/questions/68521325
复制相似问题