我不断收到 BadRequestKeyError 400,但我不知道为什么

Python 端路由


@app.route("/loginC", methods=["POST"])

def loginPage():

    valid = request.form["idnumber"]

    if valid is not None: #Creating A New User

        username = request.form["username"]

        password = request.form["password"]

        firstname = request.form["firstname"]

        lastname = request.form["lastname"]

        idnumber = request.form["idnumber"]

        logins["'"+username+"'"] = {"password":"'"+ password +"'", "firstname":"'"+ firstname +"'", "lastname":"'"+ lastname +"'", "idnumber":"'"+ idnumber +"'"}

        session["currentUser"] = username

        isLogin = True

        return redirect("/login")

    else:

        username = request.form["username"]

        password = request.form["password"]

        for account in logins:

            if username == logins:

                if logins["'"+username+"'"]["password"] == password:

                    session["currentUser"] = username

                    isLogin = True

                    return redirect("/login")

        return redirect("/login")

网页


<form action="/loginC" class="formLogin" method="post">

      <h3>Existing User</h3>

      Username: <input type="text" name="username" placeholder="username" required><br>

      Password: <input type="password" name="password" placeholder="password" required><br>

      <input type="submit" value="Login">

</form>

   <br>

   <hr class="formLogin">

   <br>

</form>

我不断收到错误:“werkzeug.exceptions.BadRequestKeyError:400 错误请求:浏览器(或代理)发送了该服务器无法理解的请求。KeyError:'idnumber'”


它请求第二个括号中的表格,又称“新用户”括号


DIEA
浏览 88回答 1
1回答

繁花如伊

代替:valid&nbsp;=&nbsp;request.form["idnumber"]改成 :如果你想检索 POST 数据:valid&nbsp;=&nbsp;request.form.get("idnumber")如果要检索 GET(查询字符串)数据:valid&nbsp;=&nbsp;request.args.get("idnumber")另外:将方法更改为@app.route("/loginC",&nbsp;methods=["GET",&nbsp;"POST"])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5