解析作为 POST 参数传递的字典列表
我有一个如下所示的 python 字典列表:
sandwiches = [
{'bread':'wheat', 'topping':'tomatoes', 'meat':'bacon'},
{'bread':'white', 'topping':'peanut butter', 'meat':'bacon'},
{'bread':'sourdough', 'topping':'cheese', 'meat':'bacon'}
]
我想将其作为 POST 参数传递给另一个 Django 应用程序。客户端应用程序需要执行哪些操作才能迭代列表?
我想做类似的事情:
for sandwich in request.POST['sandwiches']:
print "%s on %s with %s is yummy!" % (sandwich['meat'], sandwich['bread'], sandwich['topping'])
但是当我的数据到达我的客户端应用程序时,我似乎没有字典列表。
I have a list of python dictionaries that look like this:
sandwiches = [
{'bread':'wheat', 'topping':'tomatoes', 'meat':'bacon'},
{'bread':'white', 'topping':'peanut butter', 'meat':'bacon'},
{'bread':'sourdough', 'topping':'cheese', 'meat':'bacon'}
]
I want to pass this as a POST parameter to another Django app. What does the client app need to do to iterate through the list?
I want to do something like:
for sandwich in request.POST['sandwiches']:
print "%s on %s with %s is yummy!" % (sandwich['meat'], sandwich['bread'], sandwich['topping'])
But I don't seem to have a list of dicts when my data arrives at my client app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有说明如何发布到应用程序。我的建议是将字典序列化为 JSON,然后简单地发布它。
...在接收器上:
You don't say how you're POSTing to the app. My advice would be to serialize the dictionaries as JSON, then simply POST that.
... and on the receiver:
我将使用 JSON 库将字典数组序列化为单个字符串。将此字符串作为 POST 参数发送,并使用相同的库将该字符串解析回 Django 应用程序中的 python 数据类型。
http://docs.python.org/library/json.html
I would use the JSON libraries to serialize your array of dicts into a single string. Send this string as a POST param, and parse the string back into the python datatypes in the Django app using the same library.
http://docs.python.org/library/json.html
首先制作列表的字符串
现在您收到字典列表。
firstly make string of the list
Now you receive list of dictionary.