【发布时间】:2021-09-18 15:22:53
【问题描述】:
我正在使用 python 和烧瓶在项目上创建更新密码功能。用户可以通过将当前密码插入表单的第一个输入字段并将新密码插入第二个字段来更新他们的密码。问题是,一旦我单击更新按钮,我就会不断收到上面标题中的错误。 AttributeError: 'NoneType' 对象没有属性 'encode'
我在下面插入了我正在使用的python代码
@user.route("/update_password/<username>", methods=['GET', 'POST'])
def update_password(username):
user = mongo.db.users.find_one({"username": username})
if request.method == "GET":
return render_template(
"update_password.html", username=username)
if request.method == 'POST':
updated_password = generate_password_hash(
request.form.get('updated-password'))
if check_password_hash(
user['password'], request.form.get('existing-password')
):
mongo.db.users.update_one(
{'username': username},
{'$set': {'password': updated_password}}
)
flash("Password Updated Successfully")
return redirect(url_for("user.profile", username=user['session']))
else:
flash("Passwords Do Not Match")
return redirect(url_for("user.update_password", username=user['session']))
这是我用于表单的 html 代码:
<div class="row">
<div class="col-lg-8 col-md-10 offset-lg-2 offset-md-1">
<div class="card">
<div class="card-body text-center">
<h1>Update Password</h1>
<form method="POST" action="{{ url_for('user.update_password', username=username) }}">
<div class="mb-3">
<i class="fas fa-lock icon-style"></i>
<label for="existing-password" class="form-label">Enter your existing password</label>
<input type="password" name="existing-password" class="form-control" id="existing-password" required>
</div>
<!-- Password pattern from w3 Schools. See contribution in ReadMe -->
<div class="mb-3">
<i class="fas fa-lock icon-style"></i>
<label for="updated_password" class="form-label">Enter your new password</label>
<input type="password" name="updated_password" class="form-control" id="updated_password"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}"
title="Must contain at least one number, one uppercase and lowercase
letter, and at least 8 or more characters" required>
</div>
<button type="submit" class="btn btn-lg register-btn">Update</button>
</form>
</div>
</div>
</div>
</div>
在检查代码时,我的 python 函数在以下行似乎有问题
updated_password = generate_password_hash(request.form.get('updated-password'))
如果有人能提供任何有关如何解决此问题的信息,我将不胜感激
【问题讨论】:
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
标签: python-3.x flask