ballardjw2 ballardjw2 - 1 year ago 67
Javascript Question

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:

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

So now, in, I have no idea how to get the aspects of user (firstname, lastname, etc, in this case).

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.


Answer Source

In your code, it looks like form data is serialized inside the user argument which may not be what you want.

A typical pattern is to include all fields inside the form (including csrfmiddlewaretoken as a hidden field), and send the serialized form as data. E.g.

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

Then in 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download