I am fresh into this whole programming thing. Currently, I am working on a flask, python, SQLite project. I had an edit form set up to edit a listing the user posted. It worked great. Then I, copied that code to make a user info edit form for it and now I am getting TypeError: object of type 'NoneType' has no len() with essentially the exact same code.

HTML code. This code is inside a modal , if that means anything.

<form action="/edituserinfo" method="post">
                        <label for="name" style="float:left">Name:</label><br />
                        <input class="form-control" name="name" style="width:100%" placeholder="{{ user[0]['username'] }}">
                        <label for="email" style="float:left">Email:</label><br />
                        <input class="form-control" name="email" style="width:100%" placeholder="{{ user[0]['email'] }}">
                        <label for="city" style="float:left">City:</label><br />
                        <input class="form-control" name="city" style="width:100%" placeholder="{{ user[0]['city'] }}">
                    <button type="submit" class="btn btn-lg btn-success btn-block">SAVE!</button>
                </form>

Here is my python code.

@app.route("/edituserinfo", methods=["GET", "POST"])
@login_required
def edituserinfo():

    if request.method == "POST":
        user = session["user_id"]
        column = ["username", "email", "city"]
        for title in column:
            if len(request.form.get(title)) != 0:
                value = request.form.get(title)
                db.execute("UPDATE users SET %s = :edit WHERE id = :id" %(title), {"edit":value, "id":user})
                db.commit()
        return redirect("/account")

    else:
        return redirect("/account")

Any help, advice would be appreciated!! Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

The error arises because request.form.get("username") returns None.

This is because "username" is not one of the form elements in the HTML you show - I think you want "name", i.e.

column = ["name", "email", "city"]