简体   繁体   English

Flask-Restplus /路线

[英]Flask-Restplus / route

I'm trying to use Flask-Restplus to make an api and document it with swagger. 我正在尝试使用Flask-Restplus制作一个api并用昂首阔步来记录它。

This is what I have so far and it works fine except I do not know how to add a root route. 这是我到目前为止,它工作正常,但我不知道如何添加根路由。

from flask import Flask, Blueprint
from flask_restplus import Api, Resource, apidoc

app = Flask('__name__')
blueprint = Blueprint('v1', __name__, url_prefix='/rest/v1')
api = Api(blueprint, ui=False, version='1.0')

@blueprint.route('/apidoc/', endpoint='apidoc')
def swagger_ui():
   return apidoc.ui_for(api)

@blueprint.route('/', endpoint='rootres')
  def root():
     return ''

app.register_blueprint(blueprint)


ns = api.namespace('test', description='desc')

@ns.route('/', endpoint='rootresource')
class RootResource(Resource)
   def get(self):
       ...

while /rest/v1/test works fine, /rest/v1 gives me Page not found. while / rest / v1 / test工作正常,/ rest / v1给我找不到页面。

if I modify like this: 如果我像这样修改:

@blueprint.route('/aaa', endpoint='rootres')
   def root():
      return ''

then /rest/v1/aaa works. 然后/ rest / v1 / aaa有效。

Question: how can I make @blueprint.route('/') work? 问题:如何让@ blueprint.route('/')工作?

I found very similar problem. 我发现了非常相似的问题。 I wanted make '/' route with custom page, and swagger doc on different path. 我希望使用自定义页面制作'/'路线,并在不同路径上使用swagger doc。

My first attempt (Not working) 我的第一次尝试(不工作)

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)

api = Api(
    app= app,
    version='1.0',
    description='TEST API',
    doc='/docs/',
)

@app.route('/')
def hello():
    return "Hello on my page"

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

Page http://127.0.0.1:5000/ return 404 error 页面http://127.0.0.1:5000/返回404错误

Working example 工作实例

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello on my page"

api = Api(
    app= app,
    version='1.0',
    description='TEST API',
    doc='/docs/',
    default='mapi',
    default_label='Super API'
)

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

Tested on: 测试:

  • Python 3.6.1 Python 3.6.1
  • Flask (0.12.1) 烧瓶(0.12.1)
  • flask-restplus (0.10.1) flask-restplus(0.10.1)

When you wrote ui=False you disabled the /rest/v1/ path. 当您编写ui=False您禁用了/rest/v1/ path。

In the next coming release (the 0.8.1 for the end of this week), you should be able to write: 在下一个版本(本周末为0.8.1)中,您应该能够写出:

from flask import Flask, Blueprint
from flask_restplus import Api, Resource

app = Flask('__name__')
blueprint = Blueprint('v1', __name__, url_prefix='/rest/v1')
api = Api(blueprint, doc='/apidoc/', version='1.0')

@blueprint.route('/', endpoint='rootres')
def root():
    return ''

ns = api.namespace('test', description='desc')

@ns.route('/', endpoint='rootresource')
class RootResource(Resource)
    def get(self):
        ...

app.register_blueprint(blueprint)

No need anymore to register a specific view for the documentation 无需再注册文档的特定视图

For the `blueprint.route('/'), I think this is fixed by 0.8.1 too. 对于`blueprint.route('/'),我认为这也是由0.8.1修正的。

Note: register the blueprint later, after namespaces import/declaration. 注意:在命名空间导入/声明之后,稍后注册蓝图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM