我必须从我的应用中删除图片。必须从"媒体"中删除图片。文件(我的项目中的目录)和数据库。 这是我的类DeleteImage
class DeleteImage(DeleteView):
template_name = 'layout/delete_photo.html'
model = Profile
success_url = reverse_lazy('git_project:add_photo')
这是HTML页面
{% extends 'layout/base.html' %}
{% block body %}
{% if allprofiles %}
<ul>
{% for profile in allprofiles %}
<li>
<img src="/{{ profile.image }}" height="75" />
<a href="{% url 'git_project:delete_photo' user.profile.id %}">Delete</a>
</li>
</ul>
{% endfor %}
{% endif %}
{% endblock %}
我不知道你是否需要models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.FileField()
任何人都可以帮我吗?我看到了所有类似的问题和答案,但无法解决这个问题......:)
答案 0 :(得分:1)
这是我的案例中的答案。 :)
def delete(self, *args, **kwargs):
# You have to prepare what you need before delete the model
storage, path = self.image.storage, self.image.path
# Delete the model before the file
super(Profile, self).delete(*args, **kwargs)
# Delete the file after the model
storage.delete(path)
必须在model.py中的Profile类中添加。
希望,这将是有益的! :)