【问题标题】:Error when passing parameter from url to method [duplicate]将参数从 url 传递到方法时出错 [重复]
【发布时间】:2021-03-09 11:56:19
【问题描述】:

我正在为我的网络服务使用 Flask,当我尝试访问以下网址时

http://127.0.0.1:5000/2,3/5,6

我收到以下错误:

return self.view_functions[rule.endpoint](**req.view_args)
TypeError: getDistance() got an unexpected keyword argument 'startCoord'

我该如何解决这个错误?

代码

@app.route("/<string:startCoord>/<string:endCoord>",methods=['GET'] )
def getDistance():
startLat, startLng = startCoord.split(',')
end1Lat, endLng = endCoord.split(',')

print("/api.openrouteservice.org/v2/directions/driving-car?api_key=" + config['EndPoint']['api_key'] + "&start=" + startLat + "," + startLng + "&end=" + end1Lat + "," + endLng)

输出

['API_KEY']
<Section: API_KEY>

【问题讨论】:

    标签: python flask flask-restful


    【解决方案1】:

    你应该像这样定义你的路线:

    @app.route("/<string:startCoord>/<string:endCoord>",methods=['GET'] )
    def getDistance(startCoord, endCoord):
        startLat, startLng = startCoord.split(',')
        end1Lat, endLng = endCoord.split(',')
    

    【讨论】:

      【解决方案2】:

      url 编码的参数应该在函数参数中:

      @app.route("/<string:startCoord>/<string:endCoord>",methods=['GET'] )
      def getDistance(startCoord,endCoord):
      

      https://flask.palletsprojects.com/en/1.1.x/quickstart/#variable-rules

      【讨论】:

        猜你喜欢
        • 2018-05-30
        • 2014-03-25
        • 2017-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多