65

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) 
Cœur
  • 37,241
  • 25
  • 195
  • 267
chenge
  • 1,424
  • 2
  • 14
  • 19

3 Answers3

153

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.

Wyrmwood
  • 3,340
  • 29
  • 33
unmounted
  • 33,530
  • 16
  • 61
  • 61
  • 18
    +1 for a very clear example ... and for "nothing stronger than gin before breakfast" – Sean Vieira Sep 10 '10 at 15:47
  • 1
    I've got same problem with `Blueprint` =/ – holms May 05 '14 at 18:32
  • For a blueprint, replace the @app with the name of your blueprint. @app.route('xx') becomes @blueprint_name.route('xx') – Shankar ARUL Aug 18 '14 at 21:58
  • 11
    with blueprints it should be `url_for('blue_print.func_name')` as indicated in http://flask.pocoo.org/docs/0.10/blueprints/#building-urls – shuaiyuancn Jan 19 '15 at 10:26
  • I think @shuaiyuancn's comment deserves mention---apparently it's my mistake. Leaving it as a comment tends to bury it in SO's folding algorithm when it could be useful to others. – skytreader Feb 04 '15 at 11:37
  • If, like me, you still couldn't get this to work, verify that you name your Blueprint consistently. I was using `foo = Blueprint('bar')`, and wondering why `foo` was not building correctly. – jds Feb 02 '16 at 23:28
4

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!')
David Ferris
  • 2,215
  • 6
  • 28
  • 53
  • I specifically experienced this when working with a pytest decorator without using `wraps` from the `functools` library. Added blueprints and voila, problem solved. [Flask functools ?](https://stackoverflow.com/questions/14114296/why-does-flasks-url-for-throw-an-error-when-using-a-decorator-on-that-item-in-p) – Jason R Stevens CFA Dec 30 '19 at 23:59
3

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.

user1002119
  • 3,692
  • 4
  • 27
  • 30
  • I was having this issue and for whatever reason adding the dot solved it for me as well. I didn't even need to keep the dot in afterward - it's like it kicked Flask/Blueprint/Something and forced it to fix itself. Thanks again. – newfie_coder Feb 14 '19 at 14:25