I want a default picture in the model file field, my code is not working. The file field is empty.
file = models.FileField(upload_to='team_icons', null='True', blank='True',
default='pictures/picture.png')
I want a default picture in the model file field, my code is not working. The file field is empty.
file = models.FileField(upload_to='team_icons', null='True', blank='True',
default='pictures/picture.png')
Use ImageField
for storing images in objects and FileField
to save them.
in your model:
class YourModel(models.Model):
image = models.ImageField(upload_tp='team_icons', null=True, blank=True)
if your form:
class YourForm(forms.ModelForm):
image = models.FileField()
class Meta:
model = YourModel
fields = '__all__'
You need to add the default image with settings.MEDIA_ROOT
base path. MEDIA_ROOT
is absolute filesystem path to the directory that will hold user-uploaded files.
file = models.FileField(upload_to='team_icons', null='True', blank='True',
default='settings.MEDIA_ROOT/pictures/picture.png')
Also make sure that image exist at that path.