我正在尝试测试以表单提交的信息(电子邮件地址和密码)是否已存在于数据库中。如果电子邮件存在,我想检查数据库中相应的哈希密码是否与提交的密码的哈希版本匹配。这是我的登录路线的完整摘录:# Login route@app.route("/login", methods=["GET", "POST"])def login(): """Log user in""" # Forget any user_id session.clear() # User reached route via POST (as by submitting a form via POST) if request.method == "POST": # Create a cursor cur = mysql.connection.cursor() # Ensure email was submitted if not request.form.get("email"): return apology("must provide email", 403) # Ensure password was submitted elif not request.form.get("password"): return apology("must provide password", 403) # Store email in a variable email = request.form.get("email") # Query if email is within database query = cur.execute("SELECT * FROM users WHERE email = %s", (email,)) # Ensure email exists and that the password is correct if len(query) != 1 or not check_password_hash(query[0]["hashedpassword"], request.form.get("password")): return apology("invalid email and/or password", 403) # Remember which user has logged in session["user_id"] = query[0]["id"] # Redirect user to the homepage return redirect("/") # User reached route via GET (as by clicking a link or via redirect) else: return render_template("login.html")我遇到困难的部分是: # Ensure email exists and that the password is correct if len(query) != 1 or not check_password_hash(query[0]["hashedpassword"], request.form.get("password")): return apology("invalid email and/or password", 403)在这里,我尝试从数据库中提取指定电子邮件地址的单行信息。我试图使用 len 来查看是否存在单行(不能存在两行,因为我已确保电子邮件地址列在数据库中是唯一的),因此应该只有单行或没有。
添加回答
举报
0/150
提交
取消