Flask-Restplus/路线 [英] Flask-Restplus / route

查看:367
本文介绍了Flask-Restplus/路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Flask-Restplus制作一个api,并大张旗鼓地记录它.

I'm trying to use Flask-Restplus to make an api and document it with swagger.

到目前为止,这是我所拥有的,并且除了我不知道如何添加根路由之外,它都可以正常工作.

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):
       ...

/rest/v1/test正常运行时,/rest/v1给我的页面找不到.

while /rest/v1/test works fine, /rest/v1 gives me Page not found.

如果我这样修改:

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

然后/rest/v1/aaa起作用.

then /rest/v1/aaa works.

问题:如何使@ blueprint.route('/')工作?

Question: how can I make @blueprint.route('/') work?

推荐答案

编写ui=False时,您禁用了/rest/v1/路径.

When you wrote ui=False you disabled the /rest/v1/ path.

在下一个即将发布的版本(本周末的0.8.1)中,您应该能够编写:

In the next coming release (the 0.8.1 for the end of this week), you should be able to write:

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

对于blueprint.route('/'),我认为这也是由0.8.1修复的.

For the `blueprint.route('/'), I think this is fixed by 0.8.1 too.

注意:在命名空间导入/声明之后,稍后注册蓝图.

Note: register the blueprint later, after namespaces import/declaration.

这篇关于Flask-Restplus/路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆