当我使用curl在终端上执行以下命令时
curl -X POST http://myuser:mypassword@myweb.com:8000/call/make-call/ -d "tutor=1&billed=1"
我收到以下错误
/ call / make-call /的AssertionError预计
Response
, {}返回HttpResponse
或HttpStreamingResponse
查看,但收到了<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'),
)
答案 0 :(得分:5)
如果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)
答案 1 :(得分:0)
添加
#Return this if request method is not POST
return Response(json.dumps({'key': 'value'},default=json_util.default))
如果您没有在应用程序开发中构建错误代码。
我的完整代码:
@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))
答案 2 :(得分:0)
编辑如下所示的视图应该可以
@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)