Python Django-Rest-Framework AssertionError HTTPresponse 预期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23320058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:44:51  来源:igfitidea点击:

Django-Rest-Framework AssertionError HTTPresponse Expected

pythondjango-rest-framework

提问by Eddwin Paz

When i do the following command over Terminal using curl

当我使用 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, HttpResponseor HttpStreamingResponseto be returned from the view, but received a <type 'NoneType'>

AssertionError at /call/make-call/ 预期 a ResponseHttpResponseHttpStreamingResponse从视图返回,但收到一个<type 'NoneType'>

My views.py is

我的 views.py 是

@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

我的 serializer.py 是

class startCallSerializer(serializers.ModelSerializer):

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

my urls.py is

我的 urls.py 是

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

回答by Kwaw Annor

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

如果request.method == 'POST'测试失败,该函数不会返回响应。(这是一个 GET 请求)

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

回答by Haris Np

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

回答by Suraj Ghale

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)