【发布时间】:2020-12-17 23:52:00
【问题描述】:
我正在使用我的第一个 flask-RESTplus 应用程序并遇到问题。以下是我的项目的结构:
proj/
- endpoints/
- __init__.py
- example1.py
- example2.py
- app.py
这就是我的__init__.py:
from flask import Blueprint
from flask_restplus import Api
blueprint1 = Blueprint('api', __name__)
api = Api(blueprint1,version='1.0', title='Sample API',
description='A sample API',
)
ns = api.namespace('todos', description='todo')
sample1_ns = api.namespace('flask-todos')
我的example1.py 有以下代码:
from flask import Flask , request, Blueprint
from flask_restplus import Api, Resource, fields, Namespace
from endpoints import ns
todo = ns.model('Todo', {
'task': fields.String(required=True, description='The task details')
})
@ns.route('/api_route1', endpoint = 'route1-endpoint')
class Todo(Resource):
'''Shows a list of all todos, and lets you POST to add new tasks'''
@ns.doc(parser=parser)
@ns.expect(todo)
#@ns.marshal_list_with(todo)
def post(self):
#processing code
return message
我的example2.py 有以下代码:
from flask import Flask , request, Blueprint
from flask_restplus import Api, Resource, fields, Namespace]
from endpoints import sample1_ns
todo2 = sample1_ns.model('Todo2', {
'task': fields.String(required=True, description='The task details')
})
@sample1_ns.route('/api_route2', endpoint = 'route2-endpoint')
class Todos2(Resource):
'''Shows a list of all todos, and lets you POST to add new tasks'''
@sample1_ns.doc(parser=parser)
@sample1_ns.expect(todo2)
def post(self):
#processing code
return message
来自app.py,这是我尝试调用应用程序的方式:
from flask import Flask , request, Blueprint
from flask_restplus import Api, Resource, fields
from werkzeug.middleware.proxy_fix import ProxyFix
from endpoints import blueprint1
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.register_blueprint(blueprint1)
if __name__ == '__main__':
app.run(debug=True)
当我运行我的 app.py 时,它运行时没有任何错误,但是当我尝试通过转到 http://127.0.0.1:5000/api_route1 或 http://127.0.0.1:5000/api_route2 来访问我的 API 路由时,我收到 404 Page Not found 错误并显示 The requested URL was not found on the server (在邮递员APP上)
不确定错误在哪里以及如何进行更正。
注意:我有 python 3.8 和 flask-restplus 0.11.0
【问题讨论】:
标签: python flask flask-restplus