[Django] How to use persistent disk from the app

Hi everyone,

[Description]
I have Django app that uses PostgreSQL, in my DB, the admin can set Images for the field in Django model:
image = models.FileField(_(“Photo:”), upload_to=‘test_images/’, null=True, blank=True)

So, my DB saves only references to images like “test_images/image_2.svg” and it is available via a link:
mysite.com/media/test_images/image.svg

in setting.py I have the following code:
MEDIA_URL = ‘/media/’
MEDIA_ROOT = os.path.join(BASE_DIR, ‘user-media’)

[Issue]
It works well but the problem is that I lose these images after each deployment, I know why it happens. So, I bought the persistent disk in order to save the images there, and set the path there (Mount path = /test_images/) but it doesn’t work.

I have read the tonne of similar articles in this community forum and of course these docs:
Disks | Render Docs and deploy-django#static-files

However I didn’t get the answer on how to configure my app so that images would be uploaded and used from this persistent disk.

I am a newbie in Django and probably, it is obvious for most power users however I am looking for some help here.

P.s. And does anybody know how to see all files in a persistent disk? Is there any explorer or anything else?

Hi,

When creating a disk, you specify a “mount path”.

The mount path is from the root of the filesystem; it’s not relative to any folders in your code (so no BASE_DIR joins, etc. are required)

So to write media to the Render disk, set the MEDIA_ROOT to the mount path shown on the disk page.

Alan

Hi Alan,

Thank you so much for your advice.
I have changed MEDIA_ROOT =‘/test_images/’ the same like Mount path = /test_images/, and it seems it works fine.
I executed several deploys in a row, cleaned the cache, and my images were still in place.

Thank you!

P.s. Is there any way how to see all items in the persistent disk like folder structure or all files uploaded here? And how to delete obsolete files from there?