0

I am trying to make a django rest api which allows admin users to add images which the api clients can sync and display. My view responsible for creating the Clothing model, which holds a title and an image, is not working as it only saves the title but not the image.

Here is the Clothing model:

class Clothing(models.Model):
    title = models.CharField(max_length=50, blank=False)
    img = models.ImageField(blank=True, null=True, upload_to='catalog/')

Here is the view:

class AddClothingView(CreateAPIView):
    queryset = Clothing.objects.all()
    serializer_class = ClothingSerializer

And here is the serializer:

class ClothingSerializer(ModelSerializer):
    class Meta:
        model = Clothing
        fields = '__all__'

How can I fix this such that the images are saved in the catalog/ folder in my project?

Tom Finet
  • 2,056
  • 6
  • 30
  • 54
  • Can you share how you are making the request? Since `ImageField` is a file object, it is important to know if you are making a proper multipart request – arjunattam Sep 03 '17 at 09:54
  • I am not making the request from my android client. I am simply using the admin to add the image file. – Tom Finet Sep 03 '17 at 09:57
  • I'm assuming you mean django admin when you say admin. The view or serializer is not going to be called when you use the admin. After you upload the file, does the admin show a url that you can click to access the image? – arjunattam Sep 03 '17 at 10:09
  • Yes it does `catalog/IMG_4825.jpg` however when clicked I get the `404` Page not Found status code. – Tom Finet Sep 03 '17 at 10:12
  • I would check for these things 1. Click on the url and get the full location of the image. Open this location from a file explorer. Is the file there? If yes, then the admin uploader works but urls are not configured. 2. Check your static file configuration[1] (link below) - if you are using the MEDIA_ROOT setting for media uploads, then verify that your `urls.py` has the `static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)` snippet to configure media urls [1] https://docs.djangoproject.com/en/dev/howto/static-files/ – arjunattam Sep 03 '17 at 10:19
  • How do I search for the file using the full location http://XXX.XXX.X.XXX:8080/media/catalog/IMG_4825.jpg from a file explorer. I have the static configuration as you have shown. – Tom Finet Sep 03 '17 at 10:23

0 Answers0