Python 烧瓶错误:werkzeug.routing.BuildError
我修改了flaskr示例应用程序的登录,第一行出现错误。但是www.html在模板目录中Python 烧瓶错误:werkzeug.routing.BuildError,python,flask,Python,Flask,我修改了flaskr示例应用程序的登录,第一行出现错误。但是www.html在模板目录中 return redirect(url_for('www')) #return redirect(url_for('show_entries')) 显示错误: werkzeug.routing.BuildError BuildError: ('www', {}, None) return redirect(url\u for('www'))如果您在其他地方有这样一个函数,它将起作用: @app.ro
return redirect(url_for('www'))
#return redirect(url_for('show_entries'))
显示错误:
werkzeug.routing.BuildError
BuildError: ('www', {}, None)
return redirect(url\u for('www'))
如果您在其他地方有这样一个函数,它将起作用:
@app.route('/welcome')
def www():
return render_template('www.html')
@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'
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)
url\u for
查找一个函数,您将要调用的函数的名称传递给它。你可以这样想:
@app.route('/welcome')
def www():
return render_template('www.html')
@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'
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)
您还可以执行返回重定向('/someurl')
,如果这更容易记住的话。也有可能,给定第一行,您想要的只是return render\u template('www.html')
另外,不是从shuaiyuancn下面的评论来看,如果您使用的是blueprints,的
url\u应该作为('blueprint\u name.func\u name')的url\u调用。
注意,您不是传递对象,而是传递字符串 假设已经定义了def www():
(正如unmounted的精彩回答所建议的那样),如果您使用的是尚未注册的蓝图,也会引发此错误
确保在首次实例化app
时注册这些。对我来说,是这样做的:
@app.route('/welcome')
def www():
return render_template('www.html')
@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'
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)
以及在我的蓝图.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!')
我遇到了这个错误
构建错误:('project_admin',{},无)
当我接到这样的电话时
返回重定向(url\u for('project\u admin'))
在其中,我试图引用蓝图中的项目管理
功能。为了解决此错误,我在函数名称前添加了一个点,如下所示:
@app.route('/welcome')
def www():
return render_template('www.html')
@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'
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)
返回重定向(url\u用于('.project\u admin'))
瞧,我的问题解决了。+1举个非常清楚的例子。。。对于“早餐前没有比杜松子酒更烈性的东西”我对蓝图也有同样的问题。对于蓝图,用蓝图的名称替换@app@app.route('xx')变成了@blueprint\u name。route('xx')和蓝图它应该是('blueprint.func\u name')
,正如我认为@shuaiyuancn的评论值得一提的那样——显然这是我的错误。当SO的折叠算法可能对其他人有用时,将其作为一条评论,往往会将其隐藏在SO的折叠算法中。我遇到了这个问题,出于任何原因,添加点也为我解决了这个问题。我甚至不需要在事后保留这个点——就像它踢了烧瓶/蓝图/什么东西,强迫它自己修复一样。再次感谢。我在使用pytest装饰程序时特别体验到了这一点,而没有使用functools
库中的wrapps
。添加了蓝图,瞧,问题解决了。