【问题标题】:Flask/Python 3 : How to parse an url which contain associative array?Flask/Python 3:如何解析包含关联数组的 url?
【发布时间】:2018-06-04 21:11:43
【问题描述】:

我想从这个 url 获取一个元组数组,其中包含所有过滤器和页面相同。

filter = [('product_type', 'audio'),  ('feature_configuration_menu' , 'volume')]

我的网址:

curl -X GET 'http://127.0.0.1:5000/api/my_resource?filter[product_type]=audio&filter[feature_configuration_menu]=volume&page[number]=1&page[offset]=5

我使用框架烧瓶,python 3

@api.route('/my_resource', methods=['GET'])
def my_resource():
   # From `request.args` I would like this :
   # filter = [('product_type', 'audio'),  ('feature_configuration_menu' , 'volume')]

   return jsonify({'key': 'value'})

谢谢

在 PHP 中 $_GET['filter'] 非常简单,它会为您提供一个数组('product_type' => 'audio', 'feature_configuration_menu' => 'volume')。为什么 Python 这么难!?

【问题讨论】:

    标签: python url flask


    【解决方案1】:

    不确定flask 是否可以正确处理您的网址。 事件如果不完美我会那样做

    您的网址:

    http://127.0.0.1:8080/my_resource?filter=product_type-audio&filter=feature_configuration_menu-volume&page=number-1&page=offset-5
    

    WS:

    @app.route('/my_resource', methods=['GET'])
    def my_resource():
        # From `request.args` I would like this :
        # filter = [('product_type', 'audio'),  ('feature_configuration_menu' , 'volume')]
    
        filter = [tuple(filter.split('-')) for filter in request.args.getlist('filter')]
    
        return jsonify(filter)
    

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多