首页 > 解决方案 > werkzeug.routing.BuildError:无法使用值 ['token'] 为端点“forgot_passwd”构建 url。您的意思是“注销”吗?

问题描述

@app.route("/forgotpasswd",methods=["GET","POST"])
def parola_unuttum():
    form = forgotpasswd(request.form)
    sifre_adresi = form.sifre_mail.data
    if request.method == "POST" and form.validate():
        

        cursor=Mysql.connection.cursor()
        sorgu = "SELECT email from users where  email = %s"
        result = cursor.execute(sorgu,(sifre_adresi,))
        
        if result >= 1:
            token = secret.dumps(sifre_adresi, salt='forgotpasswd')
            msg = Message('Şifre değiştirme', sender='xxxxx@gmail.com', recipients=[sifre_adresi])
            link = url_for('forgot_passwd', token=token, _external=True)

            msg.body = 'Şifre değiştirme linkiniz budur ------>  {}'.format(link)
            mail.send(msg)
            flash("Birkaç dakika sonra mail ulaşacaktır.","info")
            return redirect(url_for("login"))
        else:
            flash("Mail not found! lütfen geçerli bir adres giriniz.","info")
            return render_template("forgotpasswd.html",form=form)
        return render_template("forgotpasswd.html",form=form)

令牌代码;

@app.route('/forgot_passwd/<token>',methods=['GET', 'POST'])
#@limiter.limit("1/second",error_message='Lütfen Spam Yapmayın!!')
def sifremiunuttum(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("Zaman aşımı lütfen yeniden  mail isteyiniz!","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("Parola değiştirildi","success")
            return redirect(url_for("login"))
    return render_template("yeniparola.html",form=form)

HTML 文件;

{% extends "layout.html" %}
{% block body %}
{% from "includes/formhelpers.html" import render_field %}

<form method="POST" >
    <dl>
        {{ render_field(form.sifre_mail,class="form-control") }}
        {{form.recaptcha}}
        {% for  error in form.recaptcha.errors %}
        <ul>
            <li style="color:red;"> Recaptcha Doğrulayın!</li>
        {% endfor %}
        </ul>
    </dl>
    <button type="submit" class="btn btn-secondary btn-sm">Reset</button>
    
    </form>


{% endblock body %}

当我说发送邮件时,我收到此错误
错误 werkzeug.routing.BuildError: could not build url for endpoint 'forgot_passwd' with values ['token']。您的意思是“注销”吗?
我看了一下html页面,好像没有问题。我不明白问题出在哪里。

标签: python-3.xflaskflask-mail

解决方案


flask已经为您的应用程序的所有可用路由提供了强大的内置cli命令。dump

尝试flask --help探索所有可用的命令,flask如果有的话,可能还有其他已安装的扩展(例如:dbfor flask-migrate)。

尝试flask routes --help了解命令helpflask routes

(venv) C:\myapps\flask\helloflask>flask routes --help
Usage: flask routes [OPTIONS]

  Show all registered routes with endpoints and methods.

Options:
  -s, --sort [endpoint|methods|rule|match]
                                  Method to sort routes by. "match" is the
                                  order that Flask will match routes when
                                  dispatching a request.

  --all-methods                   Show HEAD and OPTIONS methods.
  --help                          Show this message and exit.

下面是一个输出示例(应用程序有很多蓝图,每个蓝图都有自己的路线)


(venv) C:\myapps\flask\helloflask>flask routes
Endpoint            Methods    Rule
------------------  ---------  ------------------------------------------
admin.index         GET        /admin/
admin.static        GET        /admin/public/static/admin/<path:filename>
auth.login          GET, POST  /auth/login
auth.logout         GET        /auth/logout
auth.register       GET, POST  /auth/register
auth.static         GET        /auth/public/static/auth/<path:filename>
blog.archive        GET        /blog/archive
blog.author         GET        /blog/author
blog.category       GET        /blog/category
blog.index          GET        /blog/
blog.static         GET        /blog/public/static/blog/<path:filename>
blog.tag            GET        /blog/tag
contact.index       GET, POST  /contact
contact.static      GET        /public/static/contact/<path:filename>
home.about          GET        /about
home.index          GET        /
home.static         GET        /public/static/home/<path:filename>
static              GET        /public/static/<path:filename>

现在检查该路由/端点是否被重新殖民Flask(或者werkzeug准确地说)。

现在,作为一个猜测,我认为你应该改变这条线

            link = url_for('forgot_passwd', token=token, _external=True)

            link = url_for('sifremiunuttum', token=token, _external=True)

最后,我建议您查看代码。


推荐阅读