Код в 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](https://i.stack.imgur.com/hlTyG.png)
Я уже проверил, что элемент внутри request.form в файле py и name = "" внутри HTMLтот же
Я также использовал request.method == 'POST'
Так почему же возникает эта проблема?