路由层次结构,不覆盖处理程序

时间:2019-03-24 10:43:12

标签: python python-3.x flask quart

我正在编写一个烧瓶应用程序。 拥有多个端点是有意义的,

prefix + '/'
prefix + '/<id>'
prefix + '/<id>/parameters'
prefix + '/<id>/parameters/<param>'

但是,如果我试图在一个蓝图中声明它们,那么我会得到AssertionError: Handler is overwriting existing for endpoint _blueprintname_._firsthandlername_

有什么办法解决吗?我知道以前在.net core之类的技术中已经直接完成了。预先感谢。

1 个答案:

答案 0 :(得分:0)

如果您打算在路由中添加许多参数,则可以查看烧瓶的this模块。 它可以帮助您将路由分配给资源。

您可以按照以下步骤建立一组路由:

from flask import Flask, request
from flask_restful import Resource, Api, reqparse

app = Flask(__name__)
api = Api(app)

class Some(Resource):
  def get(self, id=None, params=None):
    """
      In order to allow empty params for routes, the named arguments
      must be initialized 
    """
    if id and params:
      return {'message':'this is get with id and params'}
    if id:
      return {'message':'this is get with id'}

    return {'message':'this is get'}


  def post():
    """
      One can use here reqparse module to validate post params
    """
    return {'message':'this is post'}

# Add the resource to the service 
api.add_resource(Some, '/', '/<string:id>','/<string:id>/<string:params>', ...)

# start the service
if __name__ == '__main__':
  app.run(debug=True)