Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Image #279

Closed
NadavK opened this issue Nov 13, 2013 · 2 comments
Closed

Default Image #279

NadavK opened this issue Nov 13, 2013 · 2 comments

Comments

@NadavK
Copy link

NadavK commented Nov 13, 2013

I have a model with an ImageField that I display using easy_thumbnails (|thumbnail_url).
My question is how do I display a default image if the ImageField is empty?
I would like this logic in the Model/View, NOT in the html/template.

e.g.:

DEFAULT_PICTURE = 'default.jpg'

def get_picture(self):
    if self.picture:
        return self.picture
    else:
        from DEFAULT_PICTURE

What object should get_picture() return that is compatible with easy_thumbnails?

Can you kindly provide a working example of returning an existing file to display with easy_thumbnails?

Note: I posed this on SO: http://stackoverflow.com/questions/19956837/default-image-for-django-easy-thumbnails

@SmileyChris
Copy link
Owner

Returning an ImageFieldFile (which is what self.picture is, if that's the name of your ImageField) will work fine.
Seems you got an answer to SO anyway.

@NadavK
Copy link
Author

NadavK commented Nov 14, 2013

The answer on SO is incorrect - it is not using easy_thumbnails (just returning HTML).
Your suggestion and post (http://stackoverflow.com/questions/1569698/create-return-a-default-imagefieldfile-from-inside-a-function), to create an ImageFieldFile, seems the write way, but did not work for me...
The problem is that the newly created ImageFieldFile had an empty instance.
So either set picture.instance = self
or change alias.py line 116:

    if not hasattr(target, 'instance'):
        return None

should be...

    if not hasattr(target, 'instance') or not target.instance:
        return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants