'Django Image upload issue Cloundinary
I'm learning Django at present and just about getting to the deployment stage at hit a problem serving static/media with Cloudinary, following guides how to do this, it seems straight forward enough. I've checked the Cloudinary support but I can't find anything to help, just wondering if anyone has any input.
The problem I have is that I am using Exiffield (to get image info) and ImageSpecField from ImageKit to resize the images for thumbnails and I think this is giving me problems with Cloudinary.
Firstly I can't upload images from Admin, I get an error:
Exception Value:Empty file
Secondly I get another error:
400 Client Error: Bad Request for url: https://res.cloudinary.com/ path to file
when trying to open my site (which is being served locally and in development
This is using ImageField as upload. I would change it to CloudinaryField but my model heavily relies on ExifField which wont read from it.
Model below.
class Photo(models.Model):
image = models.ImageField(upload_to='images')
mid_thumbnail = ImageSpecField(source='image',
processors=[ResizeToFit(220, 150)],
format='JPEG',
options={'quality': 100})
gallery_thumbnail = ImageSpecField(source='image',
processors=[ResizeToFit(300, 250)],
format='JPEG',
options={'quality': 100})
gallery_detail = ImageSpecField(source='image',
processors=[ResizeToFit(1000, 850)],
format='JPEG',
options={'quality': 100})
title = models.CharField(max_length=100)
albums = models.ManyToManyField(Album, blank=True, related_name='photos')
feature_image = models.BooleanField(default=False)
description = models.CharField(editable=False, max_length=150, blank=True)
posts = models.ManyToManyField(Post, blank=True, related_name='pictures')
lens = models.TextField(editable=False, max_length=100, default='Lens Data')
caption = models.CharField(editable=False, max_length=1000, default='Caption info')
file_size = models.CharField(editable=False, max_length=20, default='File_size')
width = models.IntegerField(blank=True, null=True, editable=False)
height = models.IntegerField(blank=True, null=True, editable=False)
categories = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, blank=True)
objects = models.Manager
info = models.TextField(default='**info empty**', blank=True, help_text="editable caption info no reversable")
exif = ExifField(source='image', denormalized_fields={'lens': exifgetter('LensID'),
'caption': exifgetter('Description'),
'file_size': exifgetter('FileSize'),
'description': exifgetter('Headline')
})
def save(self, *args, **kwargs):
super(Photo, self).save(*args, **kwargs)
"""Get EXIF"""
im = Image.open(self.image.path)
try:
info = im.getexif()[0x010e]
self.info = info
except KeyError:
pass
"""Save image dimensions."""
im = Image.open(pjoin(MEDIA_ROOT, self.image.name))
self.width, self.height = im.size
im.save(self.image.path)
super(Photo, self).save(*args, **kwargs)
def __str__(self):
return self.title
def albums_(self):
lst = [x[1] for x in self.albums.values_list()]
return ",".join(lst)
def size(self):
return "%s x %s" % (self.width, self.height)```
Solution 1:[1]
Thanks for that info Much appreciated. But I actually swapped to S3, which gave me same problems but with more complete error messages so i could solve the problem.
self.image.path in the model save method was the issue. I just changed it to
self.image and it worked. The problem was using absolute path to the object which is not supported by S3 or I assume Cloudinary.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rob |
