Flask Error: werkzeug.routing.BuildError

I change the login of the flaskr application example, an error is generated in the first line. But www.html is in the dir template.

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

display error:

 werkzeug.routing.BuildError BuildError: ('www', {}, None) 
+59
python flask
Sep 10 '10 at 8:15
source share
3 answers

return redirect(url_for('www')) will work if you have a function somewhere else, for example:

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

url_for looking for a function, you pass it the name of the function you want to call. Think of it this way:

 @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 can also do return redirect('/some-url') if that is easier to remember. It is also possible that you want, given your first line, just return render_template('www.html') .

And also, not from the shuaiyuancn comment below, if you are using drawings, url_for should be called as url_for('blueprint_name.func_name') Note that you are not passing an object, but rather a string. See the documentation here .

+136
Sep 10 '10 at 9:31
source share

Assuming def www(): already defined (as suggested using unmounted awesome answer), this error can also be selected if you are using a project that has not been registered.

Be sure to register them when the app first created. For me it was done like this:

 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 inside 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!') 
+3
Aug 22 '17 at 22:39 on
source share

I came across this error

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

when they called me like

return redirect(url_for('project_admin'))

in which I tried to reference the project_admin function in my Blueprint. To fix the error, I added a dot before the function name, for example like this:

return redirect(url_for('.project_admin'))

and voila, my problem has been resolved.

+2
Oct 26 '18 at 21:29
source share



All Articles