使用 flask python 检索表单输入无效

Retrieving Form Input using flask python Not working

@app.route('/signup',methods=["GET","POST"])
def signup():
    return render_template('signup.htm')
    password = request.form['password']
    if password =="python":
        return redirect(url_for("home"))

这是我的 python 代码,用于检索表单输入,然后在密码为“python”时将用户重定向到主页。

<form action="#" method="POST" name="password">
<input type="password" class="form-control" 
       id="exampleFormControlInput1" placeholder="Password">
</form>

这是注册页面的相应 HTML 代码。

您在表单属性中使用的 name="password" 是错误的。您必须在输入属性中使用它 所以HTML应该是这样的

<form action="#" method="POST"><input type="password" name="password" class="form-control" id="exampleFormControlInput1" placeholder="Password"></form>

同样在 python 中,你首先 returning 这也是错误的 return 是函数的最后一个语句

所以python代码必须是这样的

@app.route('/signup',methods=["GET","POST"])
def signup():
    if(requset.method == 'POST'):
        password = request.form['password']
        if password =="python":
            return redirect(url_for("home"))
    else:
        return render_template('signup.htm')