问题
@app.route('/forgotpasswd/<token>',methods=['GET', 'POST'])
def forgot_passwd(token):
form = Newpasswd(request.form)
password=form.passwd.data
if request.method == "POST" and form.validate():
try:
email = secret.loads(token, salt='forgotpasswd', max_age=3600)
except SignatureExpired:
flash("Timeout","danger")
return render_template("index.html")
finally:
cursor=Mysql.connection.cursor()
sorgu = "UPDATE users set password='{}' WHERE email= '{}' ".format(password,email)
cursor.execute(sorgu)
Mysql.connection.commit()
cursor.close()
flash("password changed","success")
return redirect(url_for("login"))
return render_template("newpassword.html",form=form)
I enter my new password on the newpasswd.html page and post it after entering it but it throws me back on the newpasswd.html page. I want to go to the login.html page.
回答1:
i guess you need to change your code little bit:
@app.route('/forgotpasswd/<token>',methods=['GET', 'POST'])
def forgot_passwd(token):
form = Newpasswd() # here, you don't need to pre populate your Form object
# with the request parameters, you need to validate first
# the request parameters
if form.validate_on_submit(): # here, since you are using Forms with flast-wft
password=form.passwd.data # here
try:
email = secret.loads(token, salt='forgotpasswd', max_age=3600)
except SignatureExpired:
flash("Timeout","danger")
return render_template("index.html")
finally:
# this block wont work if "secret.loads()" function fails to return
# the email, so add an if statement block
if email is none:
cursor=Mysql.connection.cursor()
sorgu = "UPDATE users set password='{}' WHERE email= '{}' ".format(password,email)
cursor.execute(sorgu)
Mysql.connection.commit()
cursor.close()
flash("password changed","success")
else:
flash("problem","danger")
return redirect(url_for("login"))
return render_template("newpassword.html",form=form)
来源:https://stackoverflow.com/questions/62640889/why-cant-i-return-to-login-html-page