[Answered ]-Passing a javascript array to a django view

1👍

Rather than getting the field values manually, you can use jQuery’s serializeArray() method, which turns a set of fields into an array which can be sent as JSON. Note that you’ll probably want both checkboxes to have the same name, so that when it’s serialised it becomes something that Django will interpret as a list.

As Steve says, to help further we’ll need to know what the view is doing and what the error is.

1👍

Got it working – I downloaded the jquery-json plugin, encoded my array as a json obect to send to django, and then used simplesjon.loads() in the django view to convert that json object to a python list. However, the fact that it took so many steps and jQuery on its own doesn’t even come with json encode functionality still makes me think there must be a more elegant way – if anyone has any insight, I’d love to hear it.

Thanks Daniel for pointing me in the right direction.

👤danny

0👍

I guess that a workaround would be transforming your array into a string, eg in javascript:

a = new Array(0,1,2,3,4);
[0, 1, 2, 3, 4]
a.join(" ")
"0 1 2 3 4"

Then you can pass this to the python back end, split it and reconstruct the data structure you need…..

Leave a comment