Flask error: werkzeug.routing.BuildError


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´╝Ü


BuildError: ('www', {}, None) 
4/15/2018 6:49:15 AM

Accepted Answer

return redirect(url_for('www')) would work if you have a function somewhere else like this:

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:

def sign_in():
    for thing in login_routine:
    return render_template('sign_in.html')

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.

6/20/2019 5:41:44 PM

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))

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')

def index():
    return render_template('index.html', page_title='My Page!')

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow