How to i get the file (where all the uploaded images are held when a user uploads an image), contained in the MEDIA_ROOT. I want to delete this file when the user deletes their account and all the uploaded images within it.
def function(request):
user_file = user_file_object
if request.method == 'POST':
# deletes the user file
user_file.delete()
user.delete()
I am sure you have defined MEDIA_URL in your settings.py file. Something like this.
MEDIA_ROOT = os.path.join(BASE_DIR,'media/')
MEDIA_URL = '/media/'
To access the MEDIA_URL in your view use below code.
from django.conf import settings
def my_view(request):
media_url = settings.MEDIA_URL
path_to_user_folder = media_url + "/user_name/"
# delete the folder.