当我从我的 Django 博客中删除帖子时,该帖子已成功删除,但与该帖子关联的所有图像仍保留在我的media文件夹中。我如何确保这些也被删除?
这是我的文件:
模型.py
class News(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
)
thumb = models.ImageField(blank=True, null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('news_detail', args=[str(self.id)])
视图.py
class NewsDeleteView(LoginRequiredMixin, DeleteView):
model = News
template_name = 'news_delete.html'
success_url = reverse_lazy('news_list')
login_url = 'login'
def test_func(self):
obj = self.get_object()
return obj.author == self.request.user
news_delete.html
{% extends 'base.html' %}
{% block content %}
<h1>Delete</h1>
<form action="" method="post">{% csrf_token %}
<p>Are you sure you want to delete "{{ news.title }}"?</p>
<button class="btn btn-danger ml-2" type="submit">Confirm</button>
</form>
{% endblock content %}
喵喵时光机
catspeake
相关分类