Python 烧瓶错误:werkzeug.routing.BuildError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3683108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:14:06  来源:igfitidea点击:

Flask error: werkzeug.routing.BuildError

pythonflask

提问by chenge

I modify the login of flaskr sample app, the first line get error. But www.html is in the template dir.

我修改了flaskr示例应用程序的登录,第一行出错。但是 www.html 在模板目录中。

return redirect(url_for('www'))
#return redirect(url_for('show_entries'))

display error:

显示错误:

werkzeug.routing.BuildError

BuildError: ('www', {}, None) 

采纳答案by unmounted

return redirect(url_for('www'))would work if you have a function somewhere else like this:

return redirect(url_for('www'))如果您在其他地方有这样的功能,它将起作用:

@app.route('/welcome')
def www():
    return render_template('www.html')

url_forlooks for a function, you pass it the nameof the function you are wanting to call. Think of it like this:

url_for寻找一个函数,您将要调用的函数的名称传递给它。可以这样想:

@app.route('/login')
def sign_in():
    for thing in login_routine:
        do_stuff(thing)
    return render_template('sign_in.html')

@app.route('/new-member')
def welcome_page():
    flash('welcome to our new members')
    flash('no cussing, no biting, nothing stronger than gin before breakfast')
    return redirect(url_for('sign_in')) # not 'login', not 'sign_in.html'

You could also do return redirect('/some-url'), if that is easier to remember. It is also possible that what you want, given your first line, is just return render_template('www.html').

你也可以这样做return redirect('/some-url'),如果这更容易记住的话。考虑到您的第一行,您想要的也可能只是return render_template('www.html').

And also, not from shuaiyuancn's comment below, if you are using blueprints, url_forshould be invoked as url_for('blueprint_name.func_name')Note you aren't passing the object, rather the string.See documentation here.

而且,不是从下面的 shuaiyuancn 的评论中,如果您使用的是蓝图,url_for则应调用为url_for('blueprint_name.func_name')注意您不是传递对象,而是字符串。请参阅此处的文档

回答by David Ferris

Assuming that def www():is already defined (as suggested by unmounted's awesome answer), this error can also be thrown if you are using a blueprint which has not been registered.

假设def www():已经定义了(正如 unmounted 的很棒的答案所建议的那样),如果您使用的是尚未注册的蓝图,也可能会引发此错误

Make sure to register these when appis first instantiated. For me it was done like this:

确保在app第一次实例化时注册这些。对我来说,它是这样完成的:

from project.app.views.my_blueprint import my_blueprint
app = Flask(__name__, template_folder='{}/templates'.format(app_path), static_folder='{}/static'.format(app_path))
app.register_blueprint(my_blueprint)

And withinmy_blueprint.py:

并且在my_blueprint.py

from flask import render_template, Blueprint
from flask_cors import CORS

my_blueprint = Blueprint('my_blueprint', __name__, url_prefix='/my-page')
CORS(my_blueprint)


@metric_retriever.route('/')
def index():
    return render_template('index.html', page_title='My Page!')

回答by user1002119

I came across this error

我遇到了这个错误

BuildError: ('project_admin', {}, None)

BuildError: ('project_admin', {}, None)

when I had a call like

当我接到一个电话时

return redirect(url_for('project_admin'))

return redirect(url_for('project_admin'))

in which I was trying to reference the project_adminfunction within my Blueprint. To resolve the error, I added a dot before the name of the function, like this:

我试图project_admin在我的蓝图中引用该函数。为了解决该错误,我在函数名称前添加了一个点,如下所示:

return redirect(url_for('.project_admin'))

return redirect(url_for('.project_admin'))

and voila, my problem was solved.

瞧,我的问题解决了。