13

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'),
)
1
  • 3
    You should use a debugger like pdb to step through your code, watch the control flow and see what is being returned by the views. Commented Apr 27, 2014 at 6:19

5 Answers 5

17

The function does not return a Response object on "GET" request. That is if request.method == 'POST' 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 Response instance if request method 
    # is not POST
    return Response({'key': 'value'}, status=status.HTTP_200_OK)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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)

Comments

0

In my case, it was resolved by reordering the methods in api_view()

Original Code:

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

Solution 1 Updated Code:

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

Or else remove other methods if not in use

Solution 2 Updated Code:

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

Comments

0

I noticed I had not added request.method and that was where the error was coming from

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.