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

时间:2019-12-17 10:28:49

标签: python flask eve

我有一个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'}

1 个答案:

答案 0 :(得分:0)

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

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