为什么我会得到TypeError:“NoneType”类型的对象没有len()错误(实际上是从我的代码的工作部分复制的代码)

2024-10-03 15:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这整件编程的事情还不熟悉。目前,我正在从事flask、python和SQLite项目。我设置了一个编辑表单来编辑用户发布的列表。效果很好。然后,我复制了该代码,为它制作了一个用户信息编辑表单,现在我得到了TypeError:“NoneType”类型的对象没有len(),基本上没有相同的代码

HTML代码。 如果这意味着什么的话,这段代码在模态中

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

这是我的python代码

@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")

任何帮助、建议都将不胜感激!! 谢谢


Tags: 代码nameformid编辑cityfortitle
1条回答
网友
1楼 · 发布于 2024-10-03 15:32:17

出现错误的原因是request.form.get("username")返回None

这是因为"username"不是您显示的HTML中的表单元素之一-我认为您需要"name",即

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

相关问题 更多 >