Python 删除Django中的图像
我必须从我的应用程序中删除图像。图像必须从“媒体”文件(我的项目中的目录)和数据库中删除。 这是我的班级形象Python 删除Django中的图像,python,django,Python,Django,我必须从我的应用程序中删除图像。图像必须从“媒体”文件(我的项目中的目录)和数据库中删除。 这是我的班级形象 class DeleteImage(DeleteView): template_name = 'layout/delete_photo.html' model = Profile success_url = reverse_lazy('git_project:add_photo') 这是HTML页面 {% extends 'layout/base.html' %
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 %}
有人能帮我吗?我看到了所有类似的问题和答案,但无法解决这个问题……:) 这是对我来说有效的答案。:) 它必须添加到model.py的Profile类中 希望,这将是有益的!:) 可能重复的
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.FileField()
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)