Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除Django中的图像_Python_Django - Fatal编程技术网

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)