如何在 Eve 应用程序中使用 add_resource?

How do I use add_resource with Eve application?

我有一个 Eve 应用程序 运行,我想重新组织它(只是为了遵循最佳实践),使其看起来类似于 Flask 应用程序目录结构。

我试过从烧瓶中使用类似的东西

from flask_restful import Api

app=Flask(__name__)
api = Api(app)
api.add_resource(resources.UserRegistration, '/registration')

像夏娃这样:

from eve import Eve

app = Eve(auth=CustomAuth)
api = Api(app)
api.add_resource(resources.UserRegistration, '/registration')

启动 Eve 应用程序时出现以下错误:

回溯:

File "/var/www/flask-api/views.py", line 1, in <module>
    from run import app
  File "/var/www/flask-api/run.py", line 22, in <module>
    api.add_resource(resources.UserRegistration, '/registration')
  File "/var/www/django/lib64/python3.6/site-packages/flask_restful/__init__.py", line 382, in add_resource
    self._register_view(self.app, resource, *urls, **kwargs)
  File "/var/www/django/lib64/python3.6/site-packages/flask_restful/__init__.py", line 448, in _register_view
    app.add_url_rule(rule, view_func=resource_func, **kwargs)
  File "/var/www/django/lib64/python3.6/site-packages/flask/app.py", line 98, in wrapper_func
    return f(self, *args, **kwargs)
  File "/var/www/django/lib64/python3.6/site-packages/flask/app.py", line 1274, in add_url_rule
    rule = self.url_rule_class(rule, methods=methods, **options)
  File "/var/www/django/lib64/python3.6/site-packages/werkzeug/routing.py", line 640, in __init__
    if not string.startswith("/"):
AttributeError: 'functools.partial' object has no attribute 'startswith'

我的 resources.py 文件看起来像这样:

from flask_restful import Resource

class UserRegistration(Resource):
    def post(self):
        return {'message': 'User registration'}

您可以使用 Flask Blueprints 在不同的文件中组织您的路由,然后在应用程序中注册它们。检查这个提到蓝图的文档:https://docs.python-eve.org/en/stable/snippets/hooks_blueprints.html.

更多关于 Flask 文档中的 Flask 蓝图:https://flask.palletsprojects.com/en/1.1.x/blueprints/