Home>

I'm currently making a blog app.

# models.py
from django.db import models
#Create your models here.
class Post (models.Model):
    title = models.CharField (max_length = 100)
    body = models.TextField (null = True, blank = True)
    date = models.DateTimeField (auto_now_add = True)
    image = models.ImageField (null = True, blank = True, upload_to = "blog_pics")
    def __str __ (self):
        return self.title
    class Meta:
        ordering = ['-date']
MEDIA_ROOT = os.path.join (BASE_DIR,'media')
MEDIA_URL ='/ media /'


I made some blog posts, but when I delete the posts on the admin site, they are deleted from the database, but not from the blog_pics directory of the media directory where the images are saved. After investigating various things, it was found that the database stores the path to save the image, and deleting the post does not delete the image itself.

When I delete a post from the Admin site, I want to delete the image at the same time. What should I do?