问题描述
我修改flaskr示例应用程序的登录名,第一行出现错误.但是www.html在模板目录中.
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'))
显示错误:
werkzeug.routing.BuildError
BuildError: ('www', {}, None)
如果您在其他地方有这样的功能,
推荐答案
return redirect(url_for('www'))
将起作用:
@app.route('/welcome')
def www():
return render_template('www.html')
url_for
寻找一个函数,将要调用的函数的 name 传递给它.这样想吧:
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'
如果更容易记住,您也可以执行return redirect('/some-url')
.给定第一行,您想要的也可能只是return render_template('www.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')
.
而且,并非来自shuaiyuancn的以下注释,如果您使用的是蓝图,则应将url_for
调用为url_for('blueprint_name.func_name')
.请注意,您不是在传递对象,而是在传递字符串. 在此处查看文档.
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')
Note you aren't passing the object, rather the string. See documentation here.
这篇关于烧瓶错误:werkzeug.routing.BuildError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!