繁体   English   中英

如何将空列表传递给参数?

[英]How to pass an empty list to the parameter?

您需要将列表发送到服务器。 如果列表不为空,则一切正常:

from flask_restful import reqparse, Resource
import requests

class CitizensPatch(Resource):
    parser = reqparse.RequestParser()
    parser.add_argument('towns', required=False, action='append', type=int)

def patch(self, a, b):
    args = self.parser.parse_args()
    print(args)  # {'requests': [1, 2, 3]}

从测试文件:

patch_json = {
    'relatives': [1, 2, 3]
}
    print(requests.patch('http://127.0.0.1:5000/a/1/b/2', json=patch_json).json())

但是,如果我们传递一个空列表,它将被解析为none类型:

from flask_restful import reqparse, Resource
import requests

class CitizensPatch(Resource):
    parser = reqparse.RequestParser()
    parser.add_argument('towns', required=False, action='append', type=int)

def patch(self, a, b):
    args = self.parser.parse_args()
    print(args)  # {'relatives': None}

从测试文件:

patch_json = {
    'relatives': []
}
print(requests.patch('http://127.0.0.1:5000/a/1/b/2', json=patch_json).json())

您需要将亲戚包裹中的数组清空为空数组,而不是无。 这个怎么做? 可以将其他一些参数传递给parser.add_argument吗?

只要将其设为可选参数,然后将其发送到html(如果列表不为空)即可。

埃格,你好! 你看《 KO》吗? 再见!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM