You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
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
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.:
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
The text was updated successfully, but these errors were encountered: