why can't I return to login html page?

只谈情不闲聊 提交于 2021-02-10 14:44:40

问题


@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!