django QueryDict只返回列表的最后一个值 [英] django QueryDict only returns the last value of a list

查看:89
本文介绍了django QueryDict只返回列表的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用django 1.8,我发现有些奇怪。
这是我的JavaScript:

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;
}

这是在views.py

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']}>

但我无法提取父母:

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.

推荐答案

来自此处


这是一个功能,而不是错误。如果要获取键的值列表,请使用以下命令:

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')

这应该有助于从django / python中的request.POST中检索列表项

这篇关于django QueryDict只返回列表的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆