lukos06
lukos06

Reputation: 312

Default image in Django model ImageField does't work

I have a huge problem with default image in my django model, in model.ImageField. My users/models.py:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=120)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

And my project/urls.py:

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

In my project/settings.py I have:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

On website, src of my image is (unknown)... Every image I upload in my admin panel is ok. Just this default, when user has no image.

What is wrong with this? I done it once in the past and everthing worked. Could you help me?

Upvotes: 1

Views: 1429

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476574

It looks for an image default.jpg in the /media/ directory. If the default.jpg is located in the profile_pics/ subdirectory, you should specify as default=… parameter profile_pics/default.jpg:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=120)
    image = models.ImageField(
        default='profile_pics/default.jpg',
        upload_to='profile_pics'
    )

Upvotes: 1

Related Questions