问题描述
我修改flaskr示例应用程序的登录,第一行出现错误。但是www.html在模板目录中。 return redirect(url_for('www'))
#return显示错误:
werkzeug.routing.BuildError
BuildError :('www',{},None)
感谢您的帮助!
解决方案 return redirect(url_for('www'))
可以工作,如果你有一个函数在这样的地方:
@ app.route('/ welcome')
def www():
return render_template('www.html')
url_for
寻找一个函数,将它传递给 你想要调用的函数。想想这样:
pre code $ @ app.route('/ login')
def sign_in():
for login_routine:
do_stuff(thing)
return render_template('sign_in.html')
@ app.route('/ new-member')
def welcome_page():
flash('欢迎来到我们的新会员')
flash('没有诅咒,没有咬,没有什么比早餐前的杜松子酒更强烈)
return redirect url_for('sign_in'))#不是'login',而是'sign_in.html'
如果这更容易记住的话,还要做 return redirect('/ some-url')
你也可以根据你的第一行,只是 return render_template('www.html')
。
另外,如果你使用的是蓝图,请不要使用shuaiyuancn的注释,而应该把 url_for
作为 url_for(blueprint_name.func_name) / code>。 。
I modify the login of flaskr sample app, the first line get error. But www.html is in the template dir.
return redirect(url_for('www'))
#return redirect(url_for('show_entries'))
display error:
werkzeug.routing.BuildError
BuildError: ('www', {}, None)
Thanks for help!
return redirect(url_for('www'))
would work if you have a function somewhere else like this:
@app.route('/welcome')
def www():
return render_template('www.html')
url_for
looks for a function, you pass it the name of the function you are wanting to call. Think of it like this:
@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')
.
And also, not from shuaiyuancn's comment below, if you are using blueprints, url_for
should be invoked as url_for(blueprint_name.func_name)
. See documentation here.
这篇关于烧瓶错误:werkzeug.routing.BuildError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!