我正在Flask中构建一个模块化应用程序,如果我从当前的Blueprint中引用另一个Blueprint中的函数,我会不断收到构建错误,例如 我有一个文件userProfiles.py
@userP.route('/myProfile/', methods=['GET'])
def showProfile():
.....
另一个文件userAccounts.py我有
@userA.route('/login/', methods=['GET', 'POST'])
def login():
.....
然后我有一个main.py来记录所有蓝图并执行app.run()
现在我正在尝试从我的showProfile函数执行url_for('userA.login),但我不断得到 - werkzeug.routing.BuildError - 。我无法解决这个问题,在线解决方案没有帮助我。
P.S。 url_for函数在我的模板中也不起作用,由于某种原因它只是没有拾取函数,我别无选择,只能href到路径。
只是为了添加更多信息,我根本没有重复功能,所有功能及其名称都是唯一的,url_for路由在每个蓝图中都能正常工作
这是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
答案 0 :(得分:7)
你是如何宣布自己的蓝图userA
的?
当您使用带有蓝图的url_for()
时,端点字符串的前缀(作为蓝图标识符)必须是蓝图的名称,因为您为第一个参数传递,而不是蓝图具有的变量名称分配给。
subapp = Blueprint('profile', __name__)
@subapp.route('/<username>')
def fetch_profile(username):
pass
如果您宣布上述蓝图,则应拨打url_for()
,如下所示:
url_for('profile.fetch_profile', username=arg)