django-imagekit
django-imagekit copied to clipboard
Cached Files Not Regenerated
Hi, I'm currently using ImageSpecField. However, the cached files disappeared and I got 404 errors instead. I don't know why caches disappear, but when they do, they are not regenerated. I did not hardcode anything link in my template.
Here is my model definition
class Photo(models.Model):
def get_upload_to(instance, filename):
return instance.title + '.' + filename.split('.')[-1]
img = models.ImageField("The Image", upload_to=get_upload_to)
thumbnail = ImageSpecField(
source="img",
format="JPEG",
processors=[ResizeToFit(height=100)],
options={'quality': 100}
)
small = ImageSpecField(
source="img",
format="JPEG",
processors=[ResizeToFit(height=200)],
options={'quality': 100}
)
medium = ImageSpecField(
source="img",
format="JPEG",
processors=[ResizeToFit(height=540)],
options={'quality': 100}
)
large = ImageSpecField(
source="img",
format="JPEG",
processors=[ResizeToFit(height=1080)],
options={'quality': 100}
)
title = models.CharField("Title", max_length=200, unique=True, blank=True)
# The title will be the link to access the photo
description = RichTextField("Description", blank=True, null=True)
is_titled = models.BooleanField("Is the photo titled (auto calculated):", default=True)
f_number = models.FloatField("f number", blank=True, null=True)
shutter_speed = models.CharField('Shutter speed', max_length=60, blank=True, null=True)
ISO = models.IntegerField("ISO", blank=True, null=True)
date_taken = models.DateField("Date Taken", blank=True, null=True)
location = models.CharField("Location", max_length=50, blank=True, null=True)
is_listed = models.BooleanField("List Link?", default=True)
is_featured = models.BooleanField("Feature on home page?", default=False)
last_modified = models.DateTimeField("Page Last Modified", auto_now=True)
def get_absolute_url(self):
return '/photo/' + self.title.replace(' ', '_')
And the relevant part in my template:
<a href="{{ photo.get_absolute_url() }}">
<img class="thumbnail" src="{{ photo.thumbnail.url }}">
</a>
I can reproduce this problem on my development machine by deleting the folder CACHE
after it is created. I think a potential solution is to run a cron that regenerate the entire cache. However, I don't know how to manually generate all caches
After deleting the CACHE
folder you need to clear the cache that stores the information that files are generated already.
Depending on the cache configured in your settings you have different options:
-
If you do not have
CACHES
in your settings this means that you are usinglocmem
by default and server restart is enough -
If You are using some other external cache (
memcached
,redis
etc.) then you need to clear it by opening the shell and executing the following:from imagekit.utils import get_cache cache = get_cache() cache.clear()
-
If you are using external cache and also you are using
django-extensions
then you can execute:python manage.py clear_cache
+1 this problem has happened to me
But it's interesting why caches disappeared !)
@ArtemBernatskyy and I already give a solution how to fix it.
Is there anyone willing to give better solution?
@vstoykov but as far as i understand it will clear WHOLE cache, isn't it ? But can we clear ONLY django-imagekit
related cache ?
Depends on how you configure your cache. If you configured imagekit to use the same cache as everything else (actually this is the default) then you will clear everything. If you want them to be separated than you need to configure different caches in settings.CACHES
and point IMAGEKIT_CACHE_BACKEND
to the other cache. Then you need to clear only that cache.
@pkqxdd @ArtemBernatskyy did you think that there is still issue with ImageKit or we can close the issue?
I can’t reproduce the issue, closing till someone will found a way to reproduce it.
What happens in production, is it the same behavior? And yes if you are using default cache locmem, restarting the server is enough to generate the cache thumbnails.