Greetings, I've been trying to make it so when I upload a new profile picture for an artist in my website, the folder where it was contained is deleted first but what I've tried doesn't seem to work. Is the server protected against that?
Here's my code:
def get_image_path(instance, filename):
if os.path.exists(os.path.join('/home/[[PATH]]/media/images', str(instance.id))):
shutil.rmtree(os.path.join('/home/[[PATH]]/media/images', str(instance.id)))
return os.path.join('media/images', str(instance.id), filename)
class Artist(models.Model):
name = models.CharField(max_length=255)
soundcloud = models.URLField(max_length=255, blank=True, null=True)
description = models.TextField()
profile_picture = models.ImageField(upload_to=get_image_path, blank=True, null=True)
current_roster = models.BooleanField(default=True)
def __str__(self):
return self.name.encode('utf8')
It doesn't seem work. Is there anyone who could help me please? The [[PATH]] is for safety purposes, it's the actual path (I don't know if it's actually dangerous to give that information but just in case).
Thank you very much!