gpt4 book ai didi

python - 使用 Flask 创建注册表单 - AttributeError : 'ValueError' object has no attribute 'name'

转载 作者:行者123 更新时间:2023-12-01 01:47:54 25 4
gpt4 key购买 nike

这几天我一直在尝试让我的注册页面正常工作。我想我已经很接近了,但现在当我尝试从登录页面单击“注册”时,我在终端中收到以下错误消息:

文件“/home/ubuntu/workspace/pset7/finance/application.py”,第 175 行,错误处理程序

return apology(e.name, e.code)

AttributeError:“ValueError”对象没有属性“name”

这是错误指向的行中的代码(我没有编写它们,它们是随分发代码一起提供的):

def errorhandler(e):
"""Handle error"""
return apology(e.name, e.code)

任何帮助将不胜感激。我将包含我的注册功能代码,以防万一有帮助:

@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":

# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 403)

# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 403)

# Ensure password == confirmation
elif request.form.get("password") != request.form.get("confirmation"):
return apology("password and confirmation do not match", 403)

# Ensure username is not already taken
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
if len(rows) == 1:
return apology("username already taken. please choose another", 403)

# Add new user to the database
session["user_id"] = db.execute("INSERT INTO users(username, hash) VALUES(:username, :hash)", username=request.form.get("username"), \
hash=generate_password_hash(request.form.get("password")))

# Redirect user to home page
return redirect("/")

谢谢!

最佳答案

我刚刚解决了!我注意到这一点:应用程序中的错误:/register [GET] 上出现异常

看来这个错误一定与 [GET] 方面有关。我查看了代码的/login 函数,它的 block 底部有以下 else 语句:

(我将login.html更改为register.html)

# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.html")

我猜问题是我正在处理 POST 请求,而不是 GET 请求。最后的 else 语句可确保为点击链接的人呈现该页面。再次感谢您的帮助!

关于python - 使用 Flask 创建注册表单 - AttributeError : 'ValueError' object has no attribute 'name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51059856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com