找不到 Flask REST 蓝图
Flask REST Blueprint not found
我正在尝试拆分在不同组件中使用 Flask、Blueprint 和 REST 的应用程序。但是我无法让 api 工作。
结构如下:
restml
|-> __init__.py
|-> app.py
\-> resources
|-> __init__.py
\-> hello.py
我的restml/__init__.py
是:
from flask import Flask
from flask_restful import Api
from restml.resources import hello
app = Flask(__name__)
app.register_blueprint(hello.blueprint)
if __name__ == '__main__':
app.run(debug=True)
我的restml/resources/hello.py
是:
from flask_restful import Api, Resource, url_for
from flask import Blueprint
blueprint = Blueprint('hello', __name__)
api = Api()
class Hello(Resource):
def get(self):
return {
"hello world"
}
api.add_resource(Hello, '/api/hello', endpoint='hello')
当我 运行 应用程序一切启动时,但 curl
无法从 url http://localhost/api/hello
.
检索
我应该如何调整文件才能找到 REST api?
更新
添加后
import restml.resources.hello import blueprint
我收到以下错误
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
web_1 | worker.init_process()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
web_1 | self.load_wsgi()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
web_1 | self.wsgi = self.app.wsgi()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
web_1 | self.callable = self.load()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
web_1 | return self.load_wsgiapp()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
web_1 | return util.import_app(self.app_uri)
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
web_1 | __import__(module)
web_1 | File "/usr/src/app/restml/__init__.py", line 4, in <module>
web_1 | from web.restml.resources.hello import blueprint
web_1 | ImportError: No module named 'web'
您需要将 restml/resources/hello.py
文件导入 restml/__init__.py
from restml/resources/hello.py import blueprint
应该可以。
我正在尝试拆分在不同组件中使用 Flask、Blueprint 和 REST 的应用程序。但是我无法让 api 工作。
结构如下:
restml
|-> __init__.py
|-> app.py
\-> resources
|-> __init__.py
\-> hello.py
我的restml/__init__.py
是:
from flask import Flask
from flask_restful import Api
from restml.resources import hello
app = Flask(__name__)
app.register_blueprint(hello.blueprint)
if __name__ == '__main__':
app.run(debug=True)
我的restml/resources/hello.py
是:
from flask_restful import Api, Resource, url_for
from flask import Blueprint
blueprint = Blueprint('hello', __name__)
api = Api()
class Hello(Resource):
def get(self):
return {
"hello world"
}
api.add_resource(Hello, '/api/hello', endpoint='hello')
当我 运行 应用程序一切启动时,但 curl
无法从 url http://localhost/api/hello
.
我应该如何调整文件才能找到 REST api?
更新
添加后
import restml.resources.hello import blueprint
我收到以下错误
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
web_1 | worker.init_process()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
web_1 | self.load_wsgi()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
web_1 | self.wsgi = self.app.wsgi()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
web_1 | self.callable = self.load()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
web_1 | return self.load_wsgiapp()
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
web_1 | return util.import_app(self.app_uri)
web_1 | File "/usr/local/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
web_1 | __import__(module)
web_1 | File "/usr/src/app/restml/__init__.py", line 4, in <module>
web_1 | from web.restml.resources.hello import blueprint
web_1 | ImportError: No module named 'web'
您需要将 restml/resources/hello.py
文件导入 restml/__init__.py
from restml/resources/hello.py import blueprint
应该可以。