Home > database >  Python Error AttributeError: 'NoneType' object has no attribute 'encode'
Python Error AttributeError: 'NoneType' object has no attribute 'encode'

Time:09-22

Im creating an update password function on a project using python and flask. The users can update their password by inserting their current password into the first input field in the form and their new password into the second field. The problem is that once I click the update button, I keep getting the error in the title above. AttributeError: 'NoneType' object has no attribute 'encode'

I have inserted the python code I am using below

@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']))

Here's the html code I am using for the form:

 <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>

When inspecting the code, it seems there may be a problem with my python function on the following line

updated_password = generate_password_hash(request.form.get('updated-password'))

I would be so grateful if anybody could give me any information on how to solve this

CodePudding user response:

I assume the error is because of field name mismatch. In the Form

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

name is updated_password

And in your route:

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

you are accessing

updated-password

which will give you None because the key name is wrong

  • Related