Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django-getlist()_Python_Django - Fatal编程技术网

Python django-getlist()

Python django-getlist(),python,django,Python,Django,我刚刚发布了这个问题,我不想在post请求中发送数组,但jQuery代码中没有问题 问题在于在django中接收POST请求。我确实喜欢这个 def portfolio_add(request): ukeys = request.POST.getlist('ukeys') ........etc....... 但我得到的ukeys值是u'[]'。当我用justrequest.POST检查时,我得到的值为u”“ 那么,如何在Django中以列表的形式获取这些值呢 谢谢 jQuer

我刚刚发布了这个问题,我不想在post请求中发送数组,但jQuery代码中没有问题

问题在于在django中接收POST请求。我确实喜欢这个

def portfolio_add(request):
    ukeys = request.POST.getlist('ukeys')
    ........etc.......
但我得到的ukeys值是
u'[]'
。当我用just
request.POST检查时,我得到的值为
u”“

那么,如何在Django中以列表的形式获取这些值呢


谢谢

jQuery POST的数组带有
[]
后缀,因为PHP和一些web框架理解这种约定,并自动在服务器端重新构建数组。Django不是这样工作的,但您应该能够通过以下方式访问数据:

ukeys = request.POST.getlist('ukeys[]')

我遇到了同样的问题——Jakub是对的,
[]
对于其他web框架很有用,但是jQuery的行为可以改变(我不喜欢
varname[]
格式)

$.param
函数专门用于此操作,它还接受一个参数来覆盖此行为

全球变化:

// put this early in your JS, preferably the first one imported after jQuery
//     or before the document.ready function if using inline js
jQuery.ajaxSettings.traditional = true; 
 $.param(data, true);  // true sets traditional mode
// for example:
 $.post('/some/url/', $.param(data, true)).done(callback)
根据每次通话进行更改:

// put this early in your JS, preferably the first one imported after jQuery
//     or before the document.ready function if using inline js
jQuery.ajaxSettings.traditional = true; 
 $.param(data, true);  // true sets traditional mode
// for example:
 $.post('/some/url/', $.param(data, true)).done(callback)

当然,您也可以像Jakub提到的那样在服务器上使用
[]

js中的
getlist
是否有等价物?我想在
tag的模板中获得通过post方法发送的相同数据。我注意到,当我打印request.post时,我不知道这是jquery功能