【问题标题】:Python Error AttributeError: 'NoneType' object has no attribute 'encode'Python 错误 AttributeError:“NoneType”对象没有属性“编码”
【发布时间】: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


【解决方案1】:

我认为错误是由于字段名称不匹配造成的。在表格中

<input type="password" name="updated_password">

名称已更新_密码

在你的路线上:

if request.method == 'POST':
    updated_password = generate_password_hash(
        request.form.get('updated-password'))

你正在访问

更新密码

这会给你 None 因为键名是错误的

【讨论】:

  • 我是个白痴。感谢您的帮助!
  • @Callan Maguire:如果有帮助,请接受我的回答。谢谢!
猜你喜欢
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 2012-04-12
  • 2023-03-03
相关资源
最近更新 更多