Answer the question
In order to leave comments, you need to log in
How to use static file in ImageField by default?
If you specify a default file in the field, then when creating new instances of the model - instead of using all instances of one file - many copies of this file are created at the root. How can this be avoided?
...
app/
└── static/
│ └── img.png # instead of using this file
...
class ModelWithDefaultStaticImage(models.Model):
image_file = models.ImageField(default='img.png')
Answer the question
In order to leave comments, you need to log in
Doing on the advice of Dr. Bacon Set
the model to Null
class officeUsers(models.Model):
....
photo = models.ImageField(verbose_name='Фотография',
upload_to='photos/%Y/%m/%d',
blank=True)
....
<div class="user_img">
<img src="{% if user.photo %}{{ user.photo.url }}{% else %}{% static 'sysadmins/img/avatar.jpg' %}{% endif %}" alt="">
</div>
Did something like this. Implemented something like this:
When creating a user, he is randomly assigned one and a static image, then this image is created with the same name in the / media / folder, if it was not there. Next, just slip the correct path of this picture in / media / into the model
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question