django-imagekit icon indicating copy to clipboard operation
django-imagekit copied to clipboard

The spec '<imagekit.specs.DynamicSpec object at 0x10db77110>' has no source file associated with it.

Open davemerwin opened this issue 9 years ago • 11 comments

I've never seen this issue before. The issue is coming up whenI try to call a cached file from a template.

My model fields:

image = models.ImageField(upload_to='fly_image', help_text="Add a picture of a fly", blank=True, null=True)
list_display_image = ImageSpecField(source='image', processors=[Transpose(),SmartResize(720, 720)], format='JPEG', options={'quality':40})
detail_display_image = ImageSpecField(source='image', processors=[Transpose(),ResizeToFit(1200, 1200, upscale=None)], format='JPEG', options={'quality':40})

The template variable:

{{ object.list_display_image.url }}

Any ideas?

davemerwin avatar Aug 27 '15 21:08 davemerwin

When the field 'list_display_image' doesn't have an image it will raise this error. My solution so far is to just create a wrapper around it. Call a function instead of the image.url directly, catch it and return a dummy/null image if it fails.

marcoacierno avatar Oct 05 '15 11:10 marcoacierno

+1 i'm having this issue for the first time today.

andrewyoung1991 avatar Oct 20 '15 02:10 andrewyoung1991

{% if object.image %} {{ object.list_display_image.url }} {% endif %} I think it'll work...

Dim4n avatar Mar 22 '16 12:03 Dim4n

I ended up using a default image in the model definition:

    image = models.ImageField(
        _("Image"),
        upload_to='uploads',
        blank=True,
        default="default.png")

That way I don't need to check if an image was set.

dezoito avatar Mar 22 '16 20:03 dezoito

can't believe this is an issue, why it is not simply returning None

stunaz avatar Apr 20 '16 22:04 stunaz

@stunaz Actually with Django if you acces url attribute if FieldFile object that is None it also raises an Exception

ValueError: The 'file' attribute has no file associated with it.

And this is the better way instead of returning None (we write in Python not in PHP).

For some reason the exception thrown from imagekit is not silenced by the template system as the exception thrown by the Django itself.

We need to check why this is happening and to change the behavior to match the Django's FieldFile. Untill then a workaround shown by @Dim4n (https://github.com/matthewwithanm/django-imagekit/issues/339#issuecomment-199787668) must be used.

vstoykov avatar Apr 21 '16 08:04 vstoykov

@vstoykov Yeah, you right, it should throw an exception, I am using it in django-rest-framework. In my serializer I just realize that I am trying directly to access the url attribute.

stunaz avatar Apr 21 '16 12:04 stunaz

Since this issue persists, this is my workaround (image is not defined in serializer!)

def to_representation(self, instance):
        data = super(ExampleSerializer, self).to_representation(instance)
        data['image'] = instance.image.url if instance.image.name else None

BojanKogoj avatar Mar 24 '17 12:03 BojanKogoj

I know it's not a good solution, but another hack

class ImageMixin(object):
    """
    Get image if it exists
    """
    def get_image(self):
        return self.image.url if self.image.name else None

And use in your model

class MyModel(models.Model, ImageMixin):

That way you can use the following in serializer

def to_representation(self, instance):
  data = super(ExampleSerializer, self).to_representation(instance)
  data['image'] = instance.get_image()

You can even use it in fields tuple

BojanKogoj avatar Apr 05 '17 07:04 BojanKogoj

+1 subscribing.

Wtower avatar Apr 26 '17 13:04 Wtower

I managed to make it work with below code

models.py

from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill, TrimBorderColor


class User(AbstractUser):
    avatar = models.ImageField(upload_to='images/avatar/',  null=True, blank=True)
    avatar_thumbnail = ImageSpecField(source='avatar',
                                      processors=[ResizeToFill(100, 100),],
                                      format='JPEG',
                                      options={'quality': 60})

serializers.py

from rest_framework import serializers
from django.contrib.auth import get_user_model

User = get_user_model()


class UserSerializer(serializers.ModelSerializer):
    avatar_thumbnail = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'avatar_thumbnail')

    def get_avatar_thumbnail(self, obj):
        if obj.avatar_thumbnail:
            return obj.avatar_thumbnail.url
        return None

kumar-student avatar Feb 19 '23 10:02 kumar-student