gpt4 book ai didi

python - Django-Rest-Framework AssertionError HTTPresponse 预期

转载 作者:太空狗 更新时间:2023-10-29 22:26:25 24 4
gpt4 key购买 nike

当我使用 curl 在终端上执行以下命令时

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

出现以下错误

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

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

我的serializer.py是

class startCallSerializer(serializers.ModelSerializer):

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

我的urls.py是

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

最佳答案

该函数不会针对“GET”请求返回Response 对象。即if request.method == 'POST' 检查不通过。

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

关于python - Django-Rest-Framework AssertionError HTTPresponse 预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23320058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com