Default image for ImageField in Django's ORM

Viewed 65404

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?

6 Answers

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.

In your image field

field = models.ImageField(upload_to='xyz',default='default.jpg')

Note : path should be inside MEDIA_ROOT.

Related