我有一个 Profile
模型如下:
class Profile(models.Model):
photo = models.ImageField(upload_to="img/users/", blank=True)
我希望我的用户能够删除他们的个人资料图片,以便
photo
中没有图片即空白。我试过
Profile.objects.get(id=1).photo.delete(save=False)
它删除了图像,但 url 仍然存在,所以我收到了 404。如何清除图像和 url?编辑:在我的模板中,我喜欢:
{% if profile.photo %}
<img src="{{ MEDIA_URL }}{{ profile.photo }}" />
{% else %}
<img src="{{ MEDIA_URL }}img/avatar.jpg" />
{% endif %}
最佳答案
更改 Profile.objects.get(id=1).photo.delete(save=False)
至 Profile.objects.get(id=1).photo.delete(save=True)
https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.fields.files.FieldFile.delete
关于django - 如何删除 Django 模型中的 ImageField 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41329858/