Why am i getting TypeError: object of type 'NoneType' has no len() error (practically copied code from a working part of my code)
Why am i getting TypeError: object of type 'NoneType' has no len() error (practically copied code from a working part of my code)
我对整个编程这件事还很陌生。目前,我正在开发一个烧瓶,python,SQLite 项目。我设置了一个编辑表单来编辑用户发布的列表。效果很好。然后我复制了该代码以为其创建用户信息编辑表单,现在我收到 TypeError: object of type 'NoneType' has no len() with essentially exact same code.
HTML 代码。
如果这意味着什么,此代码位于 modal 内。
<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")
任何帮助,建议将不胜感激!!
谢谢!
错误的产生是因为request.form.get("username")
returns None
.
这是因为 "username"
不是您显示的 HTML 中的表单元素之一 - 我认为您想要 "name"
,即
column = ["name", "email", "city"]
我对整个编程这件事还很陌生。目前,我正在开发一个烧瓶,python,SQLite 项目。我设置了一个编辑表单来编辑用户发布的列表。效果很好。然后我复制了该代码以为其创建用户信息编辑表单,现在我收到 TypeError: object of type 'NoneType' has no len() with essentially exact same code.
HTML 代码。 如果这意味着什么,此代码位于 modal 内。
<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")
任何帮助,建议将不胜感激!! 谢谢!
错误的产生是因为request.form.get("username")
returns None
.
这是因为 "username"
不是您显示的 HTML 中的表单元素之一 - 我认为您想要 "name"
,即
column = ["name", "email", "city"]