烧瓶:400错误请求:KeyError:“密码”

时间:2019-04-30 14:06:55

标签: python flask

HTML代码:

<form method="POST" action="">
    <div class="form-group" style="padding:30px 20px 0px 20px">
        <label>Username</label>
        <!-- the name variable should be the same to the item inside "request.form['name']" -->
        <input type="text" name="username" placeholder="please input your username" class="form-control" value={{request.form.username}}>
    </div>
    <div class="form-group" style="padding:0px 20px 0px 20px">
        <label>Password</label>
        <input type="password" name="password" placeholder="please input your password" class="form-control" value={{request.form.password}}>
    </div>
    <input type="submit" class="btn btn-primary" valus="Login" style="margin-left:48%;margin-top:20px;margin-bottom: 20px">
</form>

.py文件中的代码:

@app.route('/login',methods=['GET','POST'])
def login():
    if request.method=='POST':
        #get username and pwd
        username=request.form['username']
        pwd_candidate=request.form['password']
        cur=mysql.connection.cursor()
        #get user by username
        result=cur.execute("SELECT * FROM users WHERE username=%s",[username])
        #get the user info from the database,just return the first one
        if result>0:
            data=cur.fetchone()
            # we can use this line because the data is dict
            password=data['password']
            # compare pwd
            if sha256_crypt.verify(pwd_candidate,password):
                app.logger.info("You are successfully logged in")
            else:
                app.logger.info("Wrong password")
        return render_template('login.html',user=userinfo)
    return render_template('login.html',user=userinfo)

行中没有错误

<input type="text" name="username">

username=request.form['username']

但是密码有误 enter image description here

我已经检查了py文件中request.form内的项目和HTML内的name =“”是否相同

我还使用了request.method =='POST'

那为什么会发生这个问题?

0 个答案:

没有答案