Question

I'm running a django app with sorl-thumbnail and a lot of remote images (sorl loads image from external source).

Ideally I'd like to check if sorl has already cached an image in my template, and if not, load the image with ajax.

Reading the doc I found a exist method which seems to always return true

{% thumbnail item.image "160x200" crop="center" as im %}
    {% if im.exists %}
        <img src="{{ im.url }}">
    {% else %}
        <!-- load image with ajax -->
    {% endif %}
{% endthumbnail %}

Is there a method I can use to check if a image has already been cached in a template?

Was it helpful?

Solution

I ended up using a method for my model.

This is some dirty raw sql using expensive %LIKE%.

def has_image_cached(self):
        from django.db import connection
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM thumbnail_kvstore WHERE value LIKE %s",['%'+self.imageurl+'%'])
        row = cursor.fetchone()
        if row is None:
            return False
        return True
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top