【发布时间】:2012-12-03 07:57:06
【问题描述】:
我在 API 项目中使用 Flask 0.9,@router 装饰器似乎存在错误,或者我在这里做的非常错误。
我有这 2 个 URL,/twitter/authorize 和 /facebook/authorize,我正在使用 @route。问题是,当我请求 /twitter/authorize 时,实际上是 /facebook/authorize 函数在回答它。
如果我评论 /facebook/authorize 函数和路由的行,/twitter/authorize 会回答请求(应该如此)。
我尝试在 /facebook/authorize 中打印 request.path,它返回了
/twitter/授权
但是怎么可能呢,因为它在 /facebook/authorize 里面(这是请求的 url)?
两个函数的代码:
@app.route('/facebook/authorize')
def facebook_autorize():
callback = request.args.get('callback', None)
if not callback:
return error_as_json("must send callback")
scope = request.args.get('scope', 'email')
api = instantiate_facebook()
response = api.authorize(callback = callback)
response = jsonify(info = response)
response.status_code = 200
return response
@app.route('/twitter/authorize')
def twitter_autorize():
callback = request.args.get('callback', None)
api = instantiate_api()
response = api.authorize(callback = callback)
response = jsonify(info = response)
response.status_code = 200
return response
对 instantiate_api() 和 instantiate_facebook() 的调用只返回我的客户做 facebook 和 twitter 的有效实例。
当然,这两个函数都有不同的名称。我真的不明白发生了什么事。这是一个错误吗?以前有人经历过吗?如果这是一个错误,有人可以提出解决方法吗?
【问题讨论】:
-
我编辑了问题以包含一些代码。
-
Both functions, of course, have different names.- 真的吗? -
确实如此。我在这里复制/粘贴的错误(我不得不修改一些东西)。其中之一是 twitter_authorize,另一个是 facebook_authorize。我将再次编辑问题中的代码。
-
不,你是对的,乔恩。在过去的修订版中,它们确实有不同的名称。肯定是有人改了,我没注意到,只是在主库中检查了一下(我的本地版本不同)。解决了,谢谢!
标签: python web-applications routes flask