Note that there are some explanatory texts on larger screens.

plurals
  1. POImagekit - cache image not deleted after deleting original
    primarykey
    data
    text
    <p>In my project I have a model where I use Imagekit to process an image. When I save an image I have following requirements:</p> <ul> <li>rename image and thumbnail to a unique name</li> <li>when a new image is loaded, the old one should be removed (and the thumbnail in the cache should refresh to the new image).</li> </ul> <p>To accomplish this, I use following code:</p> <p>The model:</p> <pre><code>def generate_cache_filename(instance, path, specname, extension): extension = '.jpg' return 'cache/images_upload/%s_%s%s' % (instance.pk, specname, extension) def generate_image_filename_1(instance, filename): filename = '1' extension = '.jpg' return 'images_upload/%s_%s%s' % (instance.pk, filename, extension) class Model(models.Model): name = models.CharField(max_length=40) image_1 = ProcessedImageField([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(500, 370)], upload_to=generate_image_filename_1, format='JPEG', options={'quality': 90}) thumbnail_1 = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(83, 78)], image_field='image_1', cache_to=generate_cache_filename, format='JPEG', options={'quality': 90}) </code></pre> <p>The form (to delete the image when it is replaced by a new one):</p> <pre><code>if form.is_valid(): form_image = form.cleaned_data['image_1'] try: details = Model.objects.get(pk=pk) if details.image_1 != form_image: details.image_1.delete(save=False) except Model.DoesNotExist: pass form.save() </code></pre> <p>The part of renaming the images and replacing <code>image_1</code> (= loading new and deleting old) works just fine. But for some reason the <code>thumbnail_1</code> in the cache does not refresh (= is still the thumbnail of the old image). </p> <p>I think it has something to do with the deletion code in the form, but I can't figure out why and how to solve it. Someone with suggestions?</p> <p><strong>UPDATE 1</strong>: it has also something to do with the 'renaming'. I did some extra tests: when I don't rename the <code>image_1</code> file, then everything works fine (also refreshing of the thumbnail). But when I load another image with the same name, then I have the same problem: <code>image_1</code> is updated, but <code>thumbnail_1</code> is still the thumbnail of the old image.</p> <p><strong>UPDATE 2</strong>: did some more tests and when uploading a new image with the same filename, I definitely enter the <code>if statement</code> in <code>try</code>. So the old image is deleted. According to the documentation of Imagekit, the thumbnail should also be deleted. But this is not the case. </p> <p>Many thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload