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

2024-04-25 05:02:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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 function did not return a response

我知道错误,这意味着这个函数没有返回任何浏览器可以显示的内容,但我不知道漏洞在哪里。我甚至试图在每个if语句之后加上else语句,但也没用。我试着尽我所能做出回应。在

为什么这个函数没有返回任何响应,或者为什么显示错误?在

更新:

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

RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.


Tags: 函数informloggetsecretreturnif
1条回答
网友
1楼 · 发布于 2024-04-25 05:02:35

在您的代码中,POST内还有许多if,所以只需删除最后一个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')

相关问题 更多 >