为什么函数不返回响应,即使我确定它返回响应?(烧瓶)



我正在使用flask为我的网站编程一个登录页面,这是页面的脚本:

@app.route("/login/", methods = ["POST", "GET"])
def log_in():
if request.method == "POST":
attempted_username = request.form.get('username')
attempted_password = request.form.get('password')
if attempted_username == 'username':
if attempted_password == 'password':
return render_template('logged_in.html')
else:
return render_template('log_in.html')

进入页面没有问题,但是一旦我提交任何内容,无论是有效还是无效,我都会收到此错误:

值错误:视图函数未返回响应

我知道这个错误,这意味着该函数没有返回浏览器可以显示的任何内容,但我不知道洞在哪里。我什至试图在每个if语句之后放置一个else语句,这也不起作用。我试图把回应放在任何可能的地方。

为什么此函数不返回任何响应,或者为什么显示错误?

更新:

我刚刚运行了__init__.py文件,其中函数手动放置(它通常通过 apache 服务器自动放置(,他向我展示了:

运行时错误:会话不可用,因为未设置密钥。将应用程序上的secret_key设置为唯一和秘密的内容。

在你的代码中,POST许多其他如果,所以只需删除最后else

if request.method == "POST":
attempted_username = request.form.get('username')
attempted_password = request.form.get('password')
if attempted_username == 'username':
if attempted_password == 'password':
return render_template('logged_in.html')
# <--- end remove else
return render_template('log_in.html')

最新更新