cs50第9周财务.我得到一个错误,登录为注册用户没有成功



当我使用check50时,我得到了错误当我以注册帐户的身份登录时,我也不会收到任何错误

:( logging in as registered user succceeds
Cause
expected status code 200, but got 400
Log
sending POST request to /login
checking that status code 200 is returned...

然而,当我自己使用该程序时,作为注册用户登录可以很好地进行

这是我在应用程序.py 中的注册码

@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
if (request.method == "POST"):
username=request.form.get('username') 
password=request.form.get('password') 
confirmation=request.form.get('confirmation') 

if not username: #if there is no username
return apology('enter a username')
elif not password: #if there is no password
return apology('enter a password')
elif not confirmation: #if there is no confirmation
return apology('Password confirmation is required!')

if password !=confirmation: #if password is not equal to confirmation
return apology("Password does not match")

hash = generate_password_hash(password)
try:
db.execute("INSERT INTO users (username,hash) VALUES (?, ?)", username, hash)
return redirect('/')
except:
return apology("Username has allready been registered")
else:
return render_template("register.html") #display the page if no errors

这也是我的模板register.html代码上面的代码

{% extends "layout.html" %}
{% block title %}
Register
{% endblock %}
{% block main %}
<form action="/register" method="post">
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text">
</div> 




<div class="form-group">
<input class="form-control" name="password" placeholder="Password" type="password">
</div>


<div class="form-group">
<input class="form-control" name="confirmation" placeholder="Confirm Password" type="password">
</div>


<button class="btn btn-primary" type="submit">Register</button>
</form>
{% endblock %}

您必须为该check50制作index.html(表(,以标记正确的

不是一个确切的答案,但您可以尝试此解决方法。

不使用try-and-except块,而是从数据库中获取用户名为用户输入的用户名的行。然后检查是否没有行。如果没有行,那么您可以假设用户名是唯一的,然后继续。

我迟到了,但这个答案适用于那些同样面临类似问题的人。在我的情况下,这个问题发生在索引页面上,当你第一次登录时,没有什么可显示的,所以我回复了道歉,说";你没有股票"道歉是发送了400个错误代码,所以我删除了那个代码,问题就解决了。这一行只是告诉我们,当一个新用户登录时,你的应用程序显示400错误。

最新更新