Lio*_*ion 11 django jquery post
我有一个jquery客户端发送带有多维数组的POST请求,如下所示:
Run Code Online (Sandbox Code Playgroud)friends[0][id] 12345678 friends[0][name] Mr A friends[1][id] 78901234 friends[1][name] Mr B
也就是说,一个包含两个项目的数组,name和id.
是否有自动方式接收此输入作为列表或字典?我似乎无法使.getlist工作
DrMeers的链接不再有效,所以我将发布另一种实现相同功能的方法.它也不完美,如果Django内置了这样的功能会好得多.但是,因为它没有:
免责声明:我写了这篇文章.它的本质是在这个函数中,它可以更健壮,但它适用于一级对象的数组:
def getDictArray(post, name):
dic = {}
for k in post.keys():
if k.startswith(name):
rest = k[len(name):]
# split the string into different components
parts = [p[:-1] for p in rest.split('[')][1:]
print parts
id = int(parts[0])
# add a new dictionary if it doesn't exist yet
if id not in dic:
dic[id] = {}
# add the information to the dictionary
dic[id][parts[1]] = post.get(k)
return dic
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7386 次 |
| 最近记录: |