【问题标题】:Flask Restplus - The requested URL was not found on the serverFlask Restplus - 在服务器上找不到请求的 URL
【发布时间】:2019-08-16 09:48:23
【问题描述】:

我有一个烧瓶应用程序,我正在尝试使用烧瓶-restplus 和蓝图。不幸的是,我的 api 端点总是返回 在服务器上找不到请求的 URL。 即使我可以看到它存在于 app.url_map 的输出中。 项目布局如下:

- app.py
- api
   - __init__.py
   - resources.py

app.py

from api import api, api_blueprint
from api.resources import EventListResource, EventResource

app = Flask(__name__)
app.register_blueprint(api_blueprint)
db.init_app(flask_app)
app.run()

api/__init__.py

from flask_restplus import Api
from flask import Blueprint

api_blueprint = Blueprint("api_blueprint", __name__, url_prefix='/api')
api = Api(api_blueprint)

api/resources.py

from flask_restplus import Resource
from flask import Blueprint

from . import api, api_blueprint

@api_blueprint.route('/events')
class EventListResource(Resource):
    def get(self):
        "stuff"
        return items

    def post(self):
        "stuff"
        db.session.commit()
        return event, 201

应用程序启动时没有问题,我可以看到'/api/events' 出现在app.url_map 中,所以我不太确定为什么找不到该网址。任何帮助表示赞赏,谢谢!

【问题讨论】:

    标签: python flask blueprint flask-restplus


    【解决方案1】:

    Flask-RESTPlus 提供了一种使用与 Flask 蓝图几乎相同的模式的方法。主要思想是将您的应用拆分为可重用的命名空间。

    你可以这样做:

    app.py

    from flask_restplus import Api
    from api import api_namespace
    
    app = Flask(__name__)
    api = Api(app)
    db.init_app(flask_app)
    
    from api import api_namespace
    api.add_namespace(api_namespace, path='/api')
    app.run()
    

    api/init.py

    from flask_restplus import Namespace
    
    api_namespace = Namespace('api_namespace')
    

    api/resources.py

    from flask_restplus import Resource
    
    from api import api_namespace
    
    @api_namespace.route('/events')
    class EventListResource(Resource):
        def get(self):
            "stuff"
            return items
    
        def post(self):
            "stuff"
            db.session.commit()
    

    这里是文档的链接: https://flask-restplus.readthedocs.io/en/stable/scaling.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      相关资源
      最近更新 更多