I'm using an ImageField
to store profile pictures on my model.
How do I set it to return a default image if no image is defined?
default: The default value for the field. This can be a value or a callable object, in which case the object will be called every time a new record is created. null: If True , Django will store blank values as NULL in the database for fields where this is appropriate (a CharField will instead store an empty string).
ImageField in Django Forms is a input field for upload of image files. The default widget for this input is ClearableFileInput. It normalizes to: An UploadedFile object that wraps the file content and file name into a single object.
I haven't tried this, but I'm relatively sure you can just set it as a default in your field.
pic = models.ImageField(upload_to='blah', default='path/to/my/default/image.jpg')
EDIT: Stupid StackOverflow won't let me comment on other people's answers, but that old snippet is not what you want. I highly recommend django-imagekit because it does tons of great image resizing and manipulation stuff very easily and cleanly.
In models.py
image = models.ImageField(upload_to='profile_pic', default='default.jpg')
In settings.py add this:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
Now, Manually, add an image with a name ''default' into the media folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With