[Answered ]-How to access MEDIA_ROOT files within the view

2👍

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.

Leave a comment