Django POST 子词典
我通过命令行 cURL 发出以下请求:
curl -X POST http://localhost:8000/api/places/ -vvvv -d "place[name]=Starbucks"
但是,当我尝试通过调用访问参数时
request.POST.getlist('place')
,我得到一个空数组作为响应。如何访问子字典并将其传递给 ORM?
谢谢,
杰米
I'm making the following request through command-line cURL:
curl -X POST http://localhost:8000/api/places/ -vvvv -d "place[name]=Starbucks"
However, when I try to access the parameters by calling
request.POST.getlist('place')
I get an empty array as a response. How can I access the sub-dictionary which I can then pass to the ORM?
Thanks,
Jamie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTTP 数据元素不能有子元素。您发布的数据(如查询字典中所示)已被解释为具有键“place[name]”和值“Starbucks”的单个元素。因此您可以通过
request.POST["place[name]"]
获取它。HTTP data elements can't have sub-elements. The data you have posted - as shown in the querydict - has been interpreted as a single element with key "place[name]" and value "Starbucks". So you can get it with
request.POST["place[name]"]
.看起来你正在发送一个字符串,在这种情况下尝试:
如果你正在模拟一个下拉列表,你应该发送“place = Starbucks”,但是如果你试图发送一个数组,你应该尝试将字符串转换为内部数组你的Python脚本。
在您的命令中,您可以使用“-X POST”,因为参数 -d 已经是 HTTP POST:
curl 手册:
http://curl.haxx.se/docs/manual.html
It looks like you are sending a string, in that case try:
If your are simulating a dropdown list you should send "place=Starbucks", however if you are trying to send an array you should try to convert you string to an array inside your python script.
In your command you can get ride of "-X POST" as the parameter -d is already an HTTP POST:
curl manual:
http://curl.haxx.se/docs/manual.html