我想创建一个API,该API允许我通过Django Rest Framework中的APIView使用POST方法保存图像。 POST方法中保存的图像应显示在API网页的GET方法中。存储的图像不会保存在模型中,而是保存在本地目录中。
我不希望通过GET方法从目录中获取图像名称,因为我将在不久的将来将图像保存在远程服务器中。但是,我希望使过程动态化,以便GET方法可以在用户使用POSTMAN上传和提交图像后捕获/获取图像名称。该怎么办?
目前,这是我到目前为止所做的:
class API(APIView):
parser_classes = (MultiPartParser,)
#Need to display the image name here that was saved using the POST method below.
def get(self, request, *args, **kwargs):
image_filename = self.request.GET.get('image')
return Response({'image_name' : image_filename}, status=200)
def post(self, request):
#key is 'image', value is the file uploaded in the system
data = self.request.data #image that gets uploaded
img_file = data['image']
if img_file:
uploaded_file = img_file
image = [{"image_name": uploaded_file}]
return Response(image, status = 200)
else:
return Response("Please upload an image", status = 400)
POST方法工作正常,但是刷新API网址时,我得到了以下结果:
None
我期待这样的结果:
{"image_name" : <"image_filename_that's_saved_in_the_POST_method">}
任何人都可以解释出什么地方出了错,我应该采取哪些正确的步骤来实施此要求?