【问题标题】:Flask RESTPlus API URL not found error when working with Blueprint and Namespaces - 404 not found error使用蓝图和命名空间时找不到 Flask RESTPlus API URL 错误 - 404 not found 错误
【发布时间】: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_route1http://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


    【解决方案1】:

    添加方法关键字。从此:

    @sample1_ns.route('/api_route2', endpoint = 'route2-endpoint')

    到这里:

    @sample1_ns.route('/api_route2', endpoint = 'route2-endpoint', methods=['GET'])

    【讨论】:

    • 还是一样。我添加了 methods 关键字,但仍然得到相同的 404 Not Found 错误。
    【解决方案2】:

    更改如下代码即可工作 -

    proj/endpoints/__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',
    )
    
    sample1_ns = api.namespace('todos', description='todo')
    

    __init__.py:

    from flask_restplus import Api
    from proj.endpoint.example1.py import ns sample1_ns ns1
    from proj.endpoint.example2.py import ns sample1_ns ns2
    
    api = Api("Proj apis", version='1.0', title='Sample API',
        description='A sample API',
    )
    
    # register namespaces
    api.add_namespace(ns1)
    api.add_namespace(ns2)
    

    保持原来的example1.pyexample2.py

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 2014-08-01
      • 2018-12-27
      • 1970-01-01
      • 2020-01-06
      • 2018-06-13
      相关资源
      最近更新 更多