Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-Rest-Framework AssertionError HTTPresponse Expected

When i do the following command over Terminal using curl

curl -X POST http://myuser:[email protected]: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'),
)
like image 591
Eddwin Paz Avatar asked Apr 27 '14 06:04

Eddwin Paz


1 Answers

The function does not return a Response object on "GET" request. That is if request.method == 'POST' branch check does not pass.

@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)
like image 119
Kwaw Annor Avatar answered Sep 26 '22 06:09

Kwaw Annor