Reputation: 21
class Movie(models.Model):
image = models.ImageField(default='default.jpg')
title = models.CharField(max_length=255)
release_year = models.IntegerField()
number_in_stock = models.IntegerField()
daily_rate = models.FloatField()
genre = models.CharField(max_length=255)
date_created = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
The above code is my models class and i have given a default value to the ImageField .
After saving the data my image field is still empty. Whats the prooblem ?
Upvotes: 0
Views: 58
Reputation: 165
Adding on to this answer, the Media URL/Root and Static URL/Root collectively help Django process images for your application.
When you add the default image in models.py
, make sure to put '/static/image.jpg'
and once you're rending in the template load static with {% load static %}
.
Upvotes: 0
Reputation: 37
Have you added this in your setting.py :
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
Also, you will have to add the image in the static folder in your project for it work properly.
Upvotes: 1