Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask error: werkzeug.routing.BuildError

Tags:

python

flask

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) 
like image 205
chenge Avatar asked Sep 10 '10 08:09

chenge


3 Answers

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') Note you aren't passing the object, rather the string. See documentation here.

like image 159
unmounted Avatar answered Oct 06 '22 14:10

unmounted


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.

Make sure to register these when app is first instantiated. 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 within 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!')
like image 29
David Ferris Avatar answered Oct 06 '22 13:10

David Ferris


I came across this error

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

when I had a call like

return redirect(url_for('project_admin'))

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

return redirect(url_for('.project_admin'))

and voila, my problem was solved.

like image 34
user1002119 Avatar answered Oct 06 '22 14:10

user1002119