I am building a modular application in Flask, and i keep getting a build error if i refer to a function in another Blueprint from my current Blueprint e.g I have a file userProfiles.py
@userP.route('/myProfile/', methods=['GET'])
def showProfile():
.....
An in another file userAccounts.py i have
@userA.route('/login/', methods=['GET', 'POST'])
def login():
.....
And then i have a main.py that registers all the blueprints and does app.run()
now i am trying to do url_for('userA.login) from my showProfile function but i keep getting a - werkzeug.routing.BuildError - . I haven't been able to solve this and non of the solutions online have helped me.
P.S. The url_for function does not work in my templates either, for some reason it just doesn't pick up the functions, i had no choice but to href to the path.
Just to add a tad bit more information i don't hav duplicate functions at all, all the functions and their names are unique and the url_for routing works fine within each Blueprint
Here is the Traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/cevdet/PycharmProjects/FlaskProjects/jobperfect/userProfiles.py", line 126, in showProfile
else: return redirect(url_for('userA.login'))
File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 361, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 354, in url_for
force_external=external)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1607, in build
raise BuildError(endpoint, values, method)
BuildError: ('userA.login', {}, None)
127.0.0.1 - - [17/Sep/2012 23:55:12] "GET /myP
Flask – URL Building Advertisements. The url_for() function is very useful for dynamically building a URL for a specific function. The function accepts the name of a function as first argument, and one or more keyword arguments, each corresponding to the variable part of URL.
The url_for() function is used to build a URL to the specific function dynamically. The first argument is the name of the specified function, and then we can pass any number of keyword argument corresponding to the variable part of the URL.
How did you declared your blueprint userA
?
When you using url_for()
with blueprints, the prefix of endpoint string(as blueprint identifier) must be name of blueprint as you passed for first argument, not variable name that the blueprint has assigned to.
subapp = Blueprint('profile', __name__)
@subapp.route('/<username>')
def fetch_profile(username):
pass
If you declared blueprint like above, you should call url_for()
like below:
url_for('profile.fetch_profile', username=arg)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With