request.POST.getlist() Results In Single Element List

3,220 views
Skip to first unread message

Arturo

unread,
Feb 16, 2008, 8:49:09 AM2/16/08
to Django users
I was expecting my POST data to end up with a 17 element list after
using getlist(), but I end up with all the data in a single element.
I've tried to reformat the data before it's sent by removing the '[]
and different things', but I still end up with a single element.

This is my first Django and first Python project. Am I missing
something, or misunderstanding how th
e QueryDict works?


My setup is:

OSX 10.4.11
Python 2.5.1
Django version 0.97-pre-SVN-7124

Using the following to send a list to my view

template:
{% if rider_list %}
<ol id="sortedriders">
{% for r in rider_list %}
<li id="pick_{{ r.id }}">
{{ r.rider.number|stringformat:" 3d" }}
{{ r.rider.first_name }} {{ r.rider.last_name }}
</li>
{% endfor %}
</ol>
{% else %}
<p>No Riders to list</p>
{% endif %}


Sortable.create("sortedriders", {
onUpdate: function() {
new Ajax.Request("sort/", {
method: "post", asynchronous:true,
parameters: { data: Sortable.serialize("sortedriders") }
});
}
});


views.py
def sort(request):

if request.method == 'POST':

data = request.POST.getlist("data")
logging.debug(repr(data))
logging.debug(data[0])

# everything else deleted out of frustration :)

return HttpResponse('OK')



output:
DEBUG:root:
[u'sortedriders[]=22&sortedriders[]=21&sortedriders[]=25&sortedriders[]=26&sortedriders[]=23&sortedriders[]=27&sortedriders[]=28&sortedriders[]=24&sortedriders[]=29&sortedriders[]=30&sortedriders[]=31&sortedriders[]=32&sortedriders[]=33&sortedriders[]=34&sortedriders[]=35&sortedriders[]=36&sortedriders[]=37']
DEBUG:root:sortedriders[]=22&sortedriders[]=21&sortedriders[]=25&sortedriders[]=26&sortedriders[]=23&sortedriders[]=27&sortedriders[]=28&sortedriders[]=24&sortedriders[]=29&sortedriders[]=30&sortedriders[]=31&sortedriders[]=32&sortedriders[]=33&sortedriders[]=34&sortedriders[]=35&sortedriders[]=36&sortedriders[]=37

Doug B

unread,
Feb 16, 2008, 11:11:00 AM2/16/08
to Django users
I'd suggest doing a repr on request.POST and seeing exactly what is
being posted. Your use of getlist usage looks ok, so I'd guess
something is happening on the javascript side.

Arturo

unread,
Feb 16, 2008, 12:06:00 PM2/16/08
to Django users
On Feb 16, 10:11 am, Doug B <dball...@gmail.com> wrote:
> I'd suggest doing a repr on request.POST and seeing exactly what is
> being posted.  


Ok, I added logging.debug(repr(request.POST)) and got this:

DEBUG:root:<QueryDict: {u'data':
[u'sortedriders[]=22&sortedriders[]=21&sortedriders[]=24&sortedriders[]=25&sortedriders[]=23&sortedriders[]=26&sortedriders[]=27&sortedriders[]=28&sortedriders[]=29&sortedriders[]=30&sortedriders[]=31&sortedriders[]=32&sortedriders[]=33&sortedriders[]=34&sortedriders[]=35&sortedriders[]=36&sortedriders[]=37']}>


Using firebug, I can see the post data is:

data=sortedriders%5B%5D%3D22%26sortedriders%5B%5D%3D21%26sortedriders
%5B%5D%3D24%26sortedriders%5B%5D
%3D25%26sortedriders%5B%5D%3D23%26sortedriders%5B%5D
%3D26%26sortedriders%5B%5D%3D27%26sortedriders%5B
%5D%3D28%26sortedriders%5B%5D%3D29%26sortedriders%5B%5D
%3D30%26sortedriders%5B%5D%3D31%26sortedriders
%5B%5D%3D32%26sortedriders%5B%5D%3D33%26sortedriders%5B%5D
%3D34%26sortedriders%5B%5D%3D35%26sortedriders%5B%5D
%3D36%26sortedriders%5B%5D%3D37


If I run that through unquote manually, I get:

Out[95]:
'data=sortedriders[]=22&sortedriders[]=21&sortedriders[]=24&sortedriders[]=25&sortedriders[]=23&sortedriders[]=26&sortedriders[]=27&sortedriders[]=28&sortedriders[]=29&sortedriders[]=30&sortedriders[]=31&sortedriders[]=32&sortedriders[]=33&sortedriders[]=34&sortedriders[]=35&sortedriders[]=36&sortedriders[]=37'



It seems the same on both ends.



>Your use of getlist usage looks ok, so I'd guess
> something is happening on the javascript side.


Oh man, that's what I'm afraid of. I know even less about
javascript. :)

Doug B

unread,
Feb 16, 2008, 12:52:31 PM2/16/08
to Django users
> Oh man, that's what I'm afraid of. I know even less about
> javascript. :)

Javascript is something new for me too. It looks like things are
getting encoded twice. From a minute in google it looks like you are
using prototype?

Try:
postBody:Sortable.serialize('sortedriders')

instead of:
parameters: Sortable.serialize("sortedriders")

then on the django side:
request.POST.getlist('sortedriders')

This is where google led me, looks like it might have some good info.
Good luck!

http://www.willarson.com/blog/?p=39

Arturo

unread,
Feb 16, 2008, 1:39:24 PM2/16/08
to Django users


On Feb 16, 11:52 am, Doug B <dball...@gmail.com> wrote:
>
> Javascript is something new for me too.  It looks like things are
> getting encoded twice.  From a minute in google it looks like you are
> using prototype?


Yessir prototype/scriptaculous. :)



> Try:
> postBody:Sortable.serialize('sortedriders')
>
> instead of:
> parameters: Sortable.serialize("sortedriders")
>
> then on the django side:
> request.POST.getlist('sortedriders')
>
> This is where google led me, looks like it might have some good info.
> Good luck!
>
> http://www.willarson.com/blog/?p=39



YESSSS!!!!

DEBUG:root:<QueryDict: {u'sortedriders[]': [u'21', u'23', u'24',
u'22', u'25', u'26', u'27', u'28', u'29', u'30', u'31', u'32', u'33',
u'34', u'35', u'36', u'37']}>


Thank you very much!!

Funny enough, I came across that page while googlin' for an answer. I
should have read it a bit closer. D'oh!

I'll have to go back and figure out the differences later. I'm just
happy it's working after wasting *cough* 1.5 days *cough* on that.
lol

Thanks again!


Reply all
Reply to author
Forward
0 new messages