我正在使用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')
访问该页面没有问题,但是一旦我提交任何内容,无论它是有效还是无效,我都会收到此错误:
ValueError:View函数未返回响应
我知道错误,这意味着该功能没有返回浏览器可以显示的任何内容,但我不知道该漏洞在哪里。我甚至试图在每个else
语句之后放置一个if
语句,这也不起作用。我试图尽可能地回应。
为什么此函数没有返回任何响应,或显示错误的原因?
更新
我刚刚运行了__init__.py
文件,其中该函数是手动设置的(通常是自动通过apache服务器),他向我展示了:
RuntimeError:会话不可用,因为未设置密钥。将应用程序上的secret_key设置为唯一且秘密的东西。
答案 0 :(得分:0)
在您的代码中,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')