5

我正在 Flask 中构建模块化应用程序,如果我从当前蓝图中引用另一个蓝图中的函数,我会不断收到构建错误,例如我有一个文件 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 - 。我无法解决这个问题,并且没有在线解决方案对我有帮助。

PS url_for 函数在我的模板中也不起作用,由于某种原因,它只是不能使用这些函数,我别无选择,只能href 到路径。

只是为了添加更多信息,我根本没有重复的函数,所有函数及其名称都是唯一的,并且 url_for 路由在每个蓝图中都可以正常工作

这是回溯:

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
4

1 回答 1

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)
于 2014-01-27T09:23:21.930 回答