0

I have created a DRF API that allows me to submit an image using the POST method via POSTMAN. The image is not stored in the model. After it's submission, I want to view the image's name in the browser using the Django Rest Framework. After reading sources in the net, I found out that people used the GET method in order to view all the data in a model. However, I don't have a model (don't require it for now) so how can I implement this requirement?

The result should be something like this:

{ "image_name": <"name_of_the_image_stored"> } 

This is what I had done so far:

from rest_framework.views import APIView from rest_framework.response import Response from .serializers import ImgSerializer from rest_framework import status from rest_framework.parsers import FileUploadParser class ImageAPI(APIView): parser_classes = (FileUploadParser,) def post(self, request): #key is 'image' when uploading in POSTMAN file = self.request.data data = file['image'] if data: uploaded_file = data fs = FileSystemStorage(location=settings.PRIVATE_STORAGE_ROOT) filename = fs.save(uploaded_file.name, uploaded_file) data = [{"image_name": filename}] serializer = ImgSerializer(data, many = True).data return Response(serializer, status = 200) else: return Response("Please upload an image", status = 400) def get(self, request): #What should I do here in order to view the submitted image above? 

serializers.py:

from rest_framework import serializers class ImgSerializer(serializers.Serializer): image_name = serializers.CharField() 

urls.py:

from upload.views import ImageAPI from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ path("api/", ImageAPI.as_view(), name = "api"), ] urlpatterns = format_suffix_patterns(urlpatterns) 
2
  • If you don't have a model, how do you save the image_name? Commented Nov 19, 2019 at 11:23
  • I saved it in my directory. Apart from this, I used a serializer to store the image_name data. I don't intend to use a model. Commented Nov 19, 2019 at 11:24

1 Answer 1

0

First of all parser_classes should be an attribute of ImageAPI class, as I can see you've created it as a local variable, which won't do what you want. According to docs the request.data property should be a dictionary with a single key file containing the uploaded file. And regarding viewing the saved image, here you can find some ways to do that.

This is an example:

... def get(self, request, *args, **kwargs): # here I assume you've sent the name of the image using query params, # but there are other better ways to do that image_name = request.GET.get('image') # here you should read the file from your storage image_file = <read_image_file_by_given_name(image_name)> return HttpResponse(image_file, content_type='image/png') 
Sign up to request clarification or add additional context in comments.

4 Comments

I made the changes accordingly as you mentioned above. But I still don't understand how to view the name of the image using the GET method. Can you demonstrate a simple example, please?
Thank you for your reply. I did not pass the name of the image in the query params. What is wrong with your current method though? Also, I will be saving the image in a server later, if I want to view the image in the API browser, what's the best way to retrieve the image apart from using the os module?
This is just a plan, which can help you to implement what you want.
How can you read the file from the POST method instead of the storage? I don't want to access the storage in order to fetch the filename. Is it possible to save data using serializers in the POST method and call the serializer in the GET method in order to view the data?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.