4

我在定义自己的错误自定义消息时遇到问题。我不确定我是否正确执行此操作,但我正在尝试遵循文档:

http://flask-restful.readthedocs.org/en/latest/extending.html#define-custom-error-messages

这就是我的代码的样子:

from flask import Flask
from flask.ext import restful
myerrors = {
    'CannotFind':{
        'message':'We could not find it!',
        'status':404
    }
}
app = Flask(__name__)
api = restful.Api(app, errors = myerrors)

但是,当我运行此程序时,出现错误:

TypeError: __init__() got an unexpected keyword argument 'errors'

文档说Once your errors dictionary is defined, simply pass it to the Api constructor那是我没有做的吗?

我还在他们使用的文档中注意到:

api = flask_restful.Api(app, errors=errors)

所以我以为我使用了不正确的东西,因此我尝试导入该 flask_restful 但它不存在...

所以现在我很困惑,请帮助!

4

1 回答 1

8

使用 pip 安装时获得的 flask-restful 版本将是 v0.2.12

此版本没有(github链接)errors的关键字参数flask_restful.API.__init__

要获取 errors 关键字,您需要从 github 安装更新的代码。在flask-restful 安装页面上有如何做到这一点的说明。

编辑:截至 2014 年 11 月 23 日,此答案不再适用,因为 flask-restful 的 v0.3.0 已发布并且在 pypi 上。

于 2014-08-12T07:55:04.613 回答