简体   繁体   中英

Django Responds code 500 no matter what

I have a method in Django where I get POST data from a mobile app and all I do is save it and send a Response. The problem is though the the data gets saved but no matter what the app receives the response code 500.

 <Response [500]>

code:

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

I am perplexed what is the problem.
Thanks.

Almost certainly, one or more of those fields is not being sent, so you are getting a KeyError. If you set DEBUG to True you would see the traceback.

You should be using Django's forms framework, instead of directly accessing the POST data. That will validate the input and allow you to display any errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM