简体   繁体   English

如何使用具有多个键的request.POST.get()

[英]How to use request.POST.get() with multiple keys

I don't even know how to phrase this, and I'm sure it's a ridiculously stupid question, but here goes. 我什至不知道该如何措辞,我敢肯定这是一个荒谬的愚蠢问题,但这是可行的。

On my modal's html page, I'm doing: 在我的模式的html页面上,我正在做:

var user = $("#signup-form").serialize();
$.post("/signup/",
               { user : user,
                 csrfmiddlewaretoken : '{{ csrf_token }}' }

So now, in views.py, I have no idea how to get the aspects of user (firstname, lastname, etc, in this case). 所以现在,在views.py中,我不知道如何获取用户的各个方面(在这种情况下,是名字,姓氏等)。

user = request.POST.get('user')

But there doesn't seem to be a way to pull the other keys out, if that makes any sense. 但是似乎没有办法将其他键拔出。

Thanks. 谢谢。

In your code, it looks like form data is serialized inside the user argument which may not be what you want. 在您的代码中,表单数据似乎已在user参数中进行了序列化,而这可能不是您想要的。

A typical pattern is to include all fields inside the form (including csrfmiddlewaretoken as a hidden field), and send the serialized form as data. 典型的模式是包括表单内的所有字段(包括csrfmiddlewaretoken作为隐藏字段),并将序列化的表单作为数据发送。 Eg 例如

<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}"/>

Then in javascript: 然后在javascript中:

var form_data = $("#signup-form").serialize();
$.post("/signup/", form_data);

In the Django view, you can extract then extract form fields from request.POST directly, or construct a Django Form to handle this for you. 在Django视图中,您可以直接从request.POST提取然后提取表单字段,或者构造一个Django Form来为您处理。

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

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