2
votes

I am uploading files in my django project and I need an efficient method to save them. Currently I am using the below lines in settings.py to create media folder in my root directory where I save the files.

ENV_PATH = os.path.abspath(os.path.dirname(__file__))

MEDIA_ROOT = os.path.join(ENV_PATH, 'media/')

But I want to save the files inside the app directory. Is it possible to find the path to my app directory? Is it possible and how should I change the above lines to accomplish it?

Is it a proper method to add the media files into the app directory or are they stored in the root project directory ? which is the best standard here?

1

1 Answers

9
votes

That's probably not a good idea. You probably should designate a media folder with subdirectories named after each of your apps.

Media files, like static files, ideally should not be served by the same web server as your Django app, and there's probably not a clean way to serve the media dirs in each app directory by your static+media server.

Static and template files can live within app folders because templates have the APP_DIRS option, which when set to True instructs Django to load templates from within app folders, and static files are served by the manage.py runserver command and can be collected and served through the manage.py collectstatic command. It's not really possible to perform a collectstatic command for media files (technically it's possible with django-mediafiles, but it's not something I'd recommend).