Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

django QueryDict only returns the last value of a list

Using django 1.8, I'm observing something strange. Here is my javascript:

function form_submit(){
  var form = $('#form1_id');
  request = $.post($(this).attr('action'), form.serialize(), function(response){
     if(response.indexOf('Success') >= 0){
        alert(response);
     }
  },'text')
  .fail(function() {
    alert("Failed to save!");
  });
  return false;
}

and here are the parameters displayed in views.py

print request.POST
<QueryDict: {u'form_4606-name': [u''], u'form_4606-parents': [u'4603', u'2231', u'2234']}>

but I cannot extract the parents:

print request.POST['form_4606-parents']
2234

Why is it just giving me the last value? I think there is something wrong with the serialization, but I just cannot figure out how to resolve this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From here

This is a feature, not a bug. If you want a list of values for a key, use the following:

values = request.POST.getlist('key')

And this should help retrieving list items from request.POST in django/python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...