Django响应代码500无论如何

时间:2014-08-27 07:19:34

标签: python django

我在Django中有一个方法,我从移动应用程序获取POST数据,我所做的就是保存并发送响应。问题是虽然数据得到了保存,但无论应用程序收到响应代码500是什么。

 <Response [500]>

代码:

@csrf_exempt 
def store_recordings(request):
    if request.method == 'POST':
        print "In POST",request.POST
        driverName = request.POST['driverName']
        driverMobileNum = request.POST['driverMobileNum']
        customerMobileNum = request.POST['customerMobileNum']
        salesOrderNo = request.POST['salesOrderNo']
        callRecord = request.POST['callRecord']
        latitude = request.POST['latitude']
        longitude = request.POST['longitude']
        callStart = request.POST['callStart']
        callEnd = request.POST['callEnd']
        callDuration = request.POST['callDuration']
        callType = request.POST['callType']
        driverrecording = DriverRecording(driverName=driverName,driverMobileNum=driverMobileNum,customerMobileNum=customerMobileNum,salesOrderNo=salesOrderNo,callRecord=callRecord,latitude=latitude,longitude=longitude,callStart=callStart,callEnd=callEnd,callDuration=callDuration,callType=callType)
        save_or_not = driverrecording.save()
        driverexist = DriverNames.objects.all()
        new_driver_flag = False
        driverName_list = [each.driverName for each in driverexist] 
        driverName_list = list(set(driverName_list))
        if driverName in driverName_list:
            pass
        else:
            DriverNames(driverName=driverName,driverMobileNum=driverMobileNum).save()
        return HttpResponse(status=201)
    else:
        return HttpResponse(status=400)

我很困惑这是什么问题。
谢谢。

1 个答案:

答案 0 :(得分:1)

几乎可以肯定,这些字段中的一个或多个未被发送,因此您将获得KeyError。如果将DEBUG设置为True,您将看到回溯。

您应该使用Django的表单框架,而不是直接访问POST数据。这将验证输入并允许您显示任何错误。