I've created a basic files app in django so clients can upload files and copy the relative url and use it in the content of the website. One of the ideas of the app was once an entry has been removed the file would be removed from the server, to help clear space and keep the server tidy.
However when i add an entry (file) it uploads to the correct directory. However when i remove it the files remains on the server. Is there a way the file can be removed as well as the entry(file).
Here is the code:
from django.db import models
def get_upload_to(instance, filename):
if instance.file_type == 'Image':
return "images/filesApp/%s" % filename
elif instance.file_type == 'PDF':
return "pdf/filesApp/%s" % filename
return "filesApp/%s" % filename
class File(models.Model):
title = models.CharField(max_length=400, help_text="Enter the title of the file, this will appear on the listings page")
CATEGORY_CHOICES = (
('Image', 'Image'),
('PDF', 'PDF')
)
file_type = models.CharField(choices=CATEGORY_CHOICES, help_text="Please select a file type", max_length=200)
file_upload = models.FileField(upload_to=get_upload_to)
Thanks!