POST列表到django restapi

时间:2016-01-27 10:54:47

标签: python django django-rest-framework

我正在尝试发布带有一些数据的字符串列表,但无法接收服务器上的列表,它只给了我列表的最后一个元素

con = ["71qweq74520", "8324wqe57", "81ewqewq166"] 

收到

con = 81ewqewq166

在服务器API上接收的代码: -

@csrf_exempt
@api_view(['POST'])
def getUser(request):
    if request.method == 'POST':
        if isapiValid(request):
            for params in request.POST:
                print params,request.POST[params]
                #this prints last element of array/lis

python scirpt发布: -

con = ["71qweq74520", "8324wqe57", "81ewqewq166"] 
data = { 'apikey':apikey, 'sig':sig ,'con': con}
data2 = json.dumps(data)
#hostname = '127.0.0.1:8000'
hostname = 'XX.XX.XX.XX'
method = 'method'
task = 'getUser'
url = 'http://'+ hostname + '/' + method + task
r = requests.post(url, data=data)
#r = requests.post(url, data=data2) this too fails

我如何成功获取发布的列表?

Httpd日志的输出: -

 [Wed Jan 27 13:35:05.868468 2016] [:error] [pid 18858] API validation passed
    [Wed Jan 27 13:35:05.868512 2016] [:error] [pid 18858] 81ewqewq166
    [Wed Jan 27 13:35:05.868547 2016] [:error] [pid 18858] one 99qwerty99
    [Wed Jan 27 13:35:05.868566 2016] [:error] [pid 18858] apikey 4618d76f2fb84eacbac3339e5c7f2b57
    [Wed Jan 27 13:35:05.868589 2016] [:error] [pid 18858] sig e8fe50c733ec6513c91f10caf63e7864
    [Wed Jan 27 13:35:05.868608 2016] [:error] [pid 18858] con  81ew
qewq166

在request.post中收到的事情是: -

 {
            "_content_type": "application/json",
            "_content": "{\"one\": \"9998889999\",\"con\": [7106174520, 8324100257]}\r\n"
        }

但是当我执行request.POST.get('con')时,只接收到最后一个元素???

1 个答案:

答案 0 :(得分:4)

这是Django中有趣的事情之一。如果您想从帖子请求中获取列表,请使用request.POST.getlist(your_key)。在您的情况下request.POST.getlist('con')

您可以在here找到有关此主题的更多信息。