Django app - media files on persistent disk

Hello,

my Django app works fine except for my media files. I have got a persistent disk, where are my files stored, but the problem is, you can only see images and mp3 files when I have in settings.py debug=True…But in production I need to have set it to False, but I cannot see my media files then. Is there a way how to solve it? I know I can use AWS S3, but I want to have my media files on a persistent disk for now.

Thank you.

Tom

I got the same problem, I have try everything to get it to work, but no luck.
I got a Disks with the path /app/media

My Settings.py look like this:

from pathlib import Path
from decouple import config
import dj_database_url
import os

Build paths inside the project like this: BASE_DIR / ‘subdir’.

BASE_DIR = Path(file).resolve().parent.parent

SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = config(‘SECRET_KEY’)

SECURITY WARNING: don’t run with debug turned on in production!

DEBUG = True

ALLOWED_HOSTS = [‘*’]

Application definition

INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘app’, # Add this line to include the app in the project

]

MIDDLEWARE = [
‘whitenoise.middleware.WhiteNoiseMiddleware’,
‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’,
]

ROOT_URLCONF = ‘wedding.urls’

TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: [‘templates’], # Add this line to include the templates directory in the project
‘APP_DIRS’: True,
‘OPTIONS’: {
‘context_processors’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
},
},
]

WSGI_APPLICATION = ‘wedding.wsgi.application’

Database

Database configuration

DATABASES = {
‘default’: dj_database_url.config(
default=‘postgresql://postgres:postgres@localhost:5432/weddingdb’,
conn_max_age=600
)
}

Password validation

AUTH_PASSWORD_VALIDATORS = [
{
‘NAME’: ‘django.contrib.auth.password_validation.UserAttributeSimilarityValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.MinimumLengthValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.CommonPasswordValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.NumericPasswordValidator’,
},
]

Internationalization

LANGUAGE_CODE = ‘en-us’

TIME_ZONE = ‘UTC’

USE_I18N = True

USE_TZ = True

Static files (CSS, JavaScript, Images)

STATIC_URL = ‘/static/’

Static files configuration

STATIC_ROOT should always be set, regardless of DEBUG mode, to avoid issues

STATIC_ROOT = BASE_DIR / ‘staticfiles’

if DEBUG:
STATICFILES_DIRS = [
BASE_DIR / “app/static”,
]
else:
STATICFILES_STORAGE = ‘whitenoise.storage.CompressedManifestStaticFilesStorage’

MEDIA_URL = ‘/media/’
MEDIA_ROOT = ‘/app/media’

DEFAULT_AUTO_FIELD = ‘django.db.models.BigAutoField’

My problem was on my django app, I removed my venv file and created a new one, installed every library again and made few adjustments