Django media files: FileNotFound

Hello everyone,
I just deployed my django app and everything looks fine, except:

I have some files in my media directory. These are static files and never change (for some reasons, I couldn’t put them in my static folder). These files are also on my github repo and must be uploaded
with the project deploy. However, when my app tries to get access to these files, I get the following error:

FileNotFoundError
Exception Value:
[Errno 2] No such file or directory: ‘\opt\render\project\src\media\Gravity Database’

The code that is causing this error is:

dataBase_dir = os.path.join(MEDIA_ROOT, “Gravity Database”).replace(‘/’, ‘\’)
files = os.listdir(dataBase_dir)

by the way, in my settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, ‘media/’)
MEDIA_URL = ‘/media/’

I know one option for dealing with such files is to use Render Disks, however, I would like to figure out the issue with my current approach. My media folder is on my github account and I should be able to have access to them after each deploy.

Hey,

I don’t have much visibility on your deployment right now so I can only scratch the surface but here are a few things that stand out in your problem description:

  1. You’re manually replacing slashes in paths. Python’s os.path handles directory separators for you, making sure it’s correct for the operating system the code is running on.

  2. In Unix-like OS (like the ones we use at Render), the directory separator is /, not ****. The latter is for Windows. This might be why you’re having issues with accessing the directory.
    Here’s how you can modify the problematic code:

    dataBase_dir = os.path.join(MEDIA_ROOT, “Gravity Database”)files = os.listdir(dataBase_dir)

You don’t need to manually replace slashes. Let os.path.join handle it.

Jérémy.
Render Support, UTC+3

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.