I try to make avatar upload in Django project. I made changes in templates and in models, but the default image does not change to a new one. What could be the problem?
model.py
class Person(models.Model):
avatar = models.ImageField(upload_to="img", null=True, blank=True)
template.html
{% if person.avatar.url %}
<th scope="row"><img height="50" width="50" src="{{ person.avatar.url }}"
alt="no image" title="avatar">
{% else %}
<th scope="row"><img height="50" width="50" src="{% static "img/default_avatar.jpg" %}"
alt="no image" title="avatar">
{% endif %}
CodePudding user response:
Giving a default image is easier. You don't need to write an if statement every time using an avatar pic anymore
models.py:
class Person(models.Model):
avatar = models.ImageField(default="img/default.png", upload_to="img", null=True, blank=True)
template.html:
<th scope="row><img height="50" width="50" src="{{ person.avatar.url }}" alt="no image" title="avatar">
CodePudding user response:
Try
{% if person.avatar %}
<th scope="row"><img height="50" width="50" src="{{ person.avatar.url }}"
alt="no image" title="avatar">
{% else %}
<th scope="row"><img height="50" width="50" src="{% static "img/default_avatar.jpg" %}"
alt="no image" title="avatar">
{% endif %}