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

时间:2017-08-19 22:11:21

标签: python flask

我正在使用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设置为唯一且秘密的东西。

1 个答案:

答案 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')