Django POST 子词典

发布于 2024-09-07 15:37:19 字数 277 浏览 2 评论 0原文

我通过命令行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕嗳→ 2024-09-14 15:37:19

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]"].

冷心人i 2024-09-14 15:37:19

看起来你正在发送一个字符串,在这种情况下尝试:

request.POST.get('place[name]')

如果你正在模拟一个下拉列表,你应该发送“place = Starbucks”,但是如果你试图发送一个数组,你应该尝试将字符串转换为内部数组你的Python脚本。

在您的命令中,您可以使用“-X POST”,因为参数 -d 已经是 HTTP POST:

curl --help
...
-d/--data <data>   HTTP POST data (H)

curl 手册:
http://curl.haxx.se/docs/manual.html

It looks like you are sending a string, in that case try:

request.POST.get('place[name]')

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 --help
...
-d/--data <data>   HTTP POST data (H)

curl manual:
http://curl.haxx.se/docs/manual.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文