Django-Rest-Framework AssertionError HTTPresponse Expected

廉价感情. 提交于 2021-02-04 17:11:29

问题


When i do the following command over Terminal using curl

curl -X POST http://myuser:mypassword@myweb.com:8000/call/make-call/ -d "tutor=1&billed=1"

I get the following error

AssertionError at /call/make-call/ Expected a Response, HttpResponse or HttpStreamingResponse to be returned from the view, but received a <type 'NoneType'>

My views.py is

@api_view(['GET', 'POST'])
def startCall(request):

    if request.method == 'POST':

        serializer = startCallSerializer(data=request.DATA)

        if serializer.is_valid():

            serializer.save()

            return Response(serializer.data, status=status.HTTP_201_CREATED)

        else:

            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

my serializer.py is

class startCallSerializer(serializers.ModelSerializer):

    class Meta:
        model = call
        fields = ('tutor', 'billed', 'rate', 'opentok_sessionid')

my urls.py is

urlpatterns = patterns(
    'api.views',
    url(r'^call/make-call/$','startCall', name='startCall'),
)

回答1:


The function does not return a Response if the request.method == 'POST' test fail. (That is on a GET request)

@api_view(['GET', 'POST'])
def startCall(request):

    if request.method == 'POST':
        serializer = startCallSerializer(data=request.DATA)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
             return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    #Return this if request method is not POST
    return Response({'key': 'value'}, status=status.HTTP_200_OK)



回答2:


Just add

#Return this if request method is not POST
    return Response(json.dumps({'key': 'value'},default=json_util.default))

if you don't have an error code built in your application development.

My full code :

@csrf_exempt
@api_view(['GET','POST'])
def uploadFiletotheYoutubeVideo(request):
    if request.method == 'POST': 
        file_obj = request.FILES['file']#this is how Django accepts the files uploaded. 
        print('The name of the file received is ')
        print(file_obj.name)
        posteddata = request.data
        print("the posted data is ")
        print(posteddata)
        response = {"uploadFiletotheYoutubeVideo" : "uploadFiletotheYoutubeVideo"}
        return Response(json.dumps(response, default=json_util.default))
    #Return this if request method is not POST
    return Response(json.dumps({'key': 'value'},default=json_util.default))



回答3:


Editing the views like below should work

@api_view(['GET', 'POST'])
def startCall(request):
    if request.method == 'POST':
    serializer = startCallSerializer(data=request.data)
    data={}
    if serializer.is_valid():
        datas = serializer.save()
        data['tutor']=datas.tutor
        data['billed']=datas.billed
        data['rate']=datas.rate


    else:
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    return Response(data)


来源:https://stackoverflow.com/questions/23320058/django-rest-framework-assertionerror-httpresponse-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!