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

问题描述 投票:0回答:1

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")

HTML

<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 action="/loginC" class="formLogin" method="post">
      <h3>New User</h3>
      Username: <input type="text" name="username" placeholder="username" required><br>
      Password: <input type="password" name="password" placeholder="password" required> <br>
      Firstname: <input type="text" name="firstname" placeholder="firstname" required><br>
      Lastname: <input type="text" name="lastname" placeholder="lastname" required><br>
      Student ID: <input name="idnumber" type="text" placeholder="ID number" required><br>
      <input type="submit" value="Create">
</form>

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

它在第二个表格中要求括号,也就是“新用户”括号

python html flask routing bad-request
1个回答
0
投票

代替:

valid = request.form["idnumber"]

更改为:

如果要检索POST数据:

valid = request.form.get("idnumber")

如果要检索GET(查询字符串)数据:

valid = request.args.get("idnumber")

也:将方法更改为

@app.route("/loginC", methods=["GET", "POST"])
热门问题
推荐问题
最新问题