【问题标题】:what is the use of endpoint parameter in add_resource() in flask restful api?烧瓶restful api中add_resource()中端点参数的用途是什么?
【发布时间】:2019-10-24 06:43:37
【问题描述】:

我写了两个api,一个是创建设备(POST),另一个是列出所有设备(GET)。

in app.py
---------
# Get the List of the devices
api.add_resource(DeviceDetail, "/v1/t/device/")
# Create new Devices
api.add_resource(DeviceDetail, "/v1/t/device/create")

in controller/device.py
class DeviceDetail(Resource):
      def get(): 
         #some code to list out all the device and return the response
      def post():
         #some code to insert the record to db and return the response

AssertionError: View function mapping is overwriting an existing endpoint function: devicedetail.

如果我将端点参数传递给 add_resource() 那么我没有收到任何错误。

# Get the List of the devices
api.add_resource(DeviceDetail, "/v1/t/device/",endpoint='get_the_list_of_deives')
# Create new Devices
api.add_resource(DeviceDetail, "/v1/t/device/create",endpoint='create_the_deivce')

所以我很困惑,它在这里做了什么。

【问题讨论】:

    标签: python python-3.x flask flask-restful


    【解决方案1】:

    Flask documentation 说:

    endpoint(str) – 端点名称(默认为 Resource.name.lower() 可用于在 fields.Url 字段中引用此路由

    如果您不提供端点,Flask 将为您创建默认端点,并且您的错误清楚地说明了问题所在。 View function mapping is overwriting an existing endpoint function: devicedetail

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多