python - TypeError:需要一个类似字节的对象,而不是“RowProxy”
问题描述
我正在尝试在我的 python web 应用程序中实现登录/注册/注销元素。我正在使用烧瓶。我正在使用 bcrypt 对密码进行散列和加盐,但不断收到此错误:TypeError: a bytes-like object is required, not 'RowProxy'。密码肯定会存储在哈希和盐中。但不允许用户登录。
@app.route("/searchPage", methods=['POST','GET'])
def loggingin():
title = "Search"
#get request form variables
username = request.form.get('username')
if db.execute("SELECT username FROM users WHERE username = :username",{"username": username}).rowcount == 0:
return render_template("login.html", message="invalid username, please try again.")
hashed_password = db.execute("SELECT username, password FROM users WHERE username = :username",{"username": username}).fetchone()
if bcrypt.checkpw(request.form.get('password'), hashed_password):
return render_template("searchPage.html", title=title)
else:
return render_template("login.html", message="Incorrect Password.")
我的html是:
{% extends "nav.html" %}
{% block body %}
<main>
<h1>{{ message }}</h1>
<h3>Log In </h3>
<form action="{{ url_for('loggingin') }}" method="POST">
<div class="form-group">
<label>Username</label>
<input class="form-control" type="text" name="username">
</div>
<div class="form-group">
<label>Password</label>
<input class="form-control" type="password" name="password">
</div>
<button class="btn btn-success" type="submit">Log In</button>
</form>
</main>
{% endblock %}
解决方案
db.execute("SELECT username, password FROM users WHERE username = :username",{"username": username}).fetchone()['password']
不同之处在于这一行末尾的 ['password']。@mechanical_meat
推荐阅读
- sed - 匹配一个子字符串并在同一行替换另一个
- java - How to implement autocomplete in android with data being in cache and synced with firestore(java)
- amazon-web-services - 在 /root_efs 或 ~/user_efs 上安装 AWS EFS 的影响
- java - 将一种格式的String类型的Date转换为另一种Date格式的String类型
- junit5 - 在测试用例中断言 System.out/err
- python - Django - 如果错误,视图函数内部的函数会自动向用户返回响应吗?
- python - 如何检查列中的日期是否是每月的第二个星期一并在 Python 中返回 True/False?
- jboss - NameNotFoundException 与 jboss 到 Wildfly 服务器迁移
- java - 在链表Java中插入一个位置
- python - 我需要帮助编写一个程序,该程序使用嵌套循环打印出彼此相邻的两个形状