Static and Media files do not work after splitting settings

Hey,

I want to get my minimum Django project up and running but I am struggling with settings splitting.

I have chosen the approach of having a settings module (i think it is called that way):
I am in a folder called django-landscape and created a base.py, local.py and prod.py

.
├── landscape
│   ├── settings
│   │   ├── base.py
│   │   ├── __init__.py
│   │   ├── local.py
│   │   ├── prod.py

Now this works perfectly with specifying the settings file which I want to use:

python manage.py runserver --settings landscape.settings.local
python manage.py runserver --settings landscape.settings.prod

The Problem - Static and media files are not found

Here is the complete base.py as reference:

"""
Django settings for landscape project.

Generated by 'django-admin startproject' using Django 4.0.3.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
from pathlib import Path

import environ

env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False),
    SECRET_KEY=(str, 'django-insecure-ur5y7+d#j9)j)34wd=sow*c-d=at4i3si5dn590&@p3wz2ynnw'),
    ALLOWED_HOSTS=(list, ['localhost', '127.0.0.1']),
    DATABASE_HOST=(str, 'postgres'),
    DATABASE_USER=(str, 'postgres'),
    DATABASE_PASSWORD=(str, 'postgres'),
    DATABASE_PORT=(int, 9000),
    DATABASE_NAME=(str, 'postgres')

)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
print(BASE_DIR)


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env('DEBUG')

ALLOWED_HOSTS = env('ALLOWED_HOSTS')


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions',
    'self_hosted.apps.SelfHostedConfig',
]

MIDDLEWARE = [
    '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 = 'landscape.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'landscape.wsgi.application'

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': env('DATABASE_NAME'),
        'USER': env('DATABASE_USER'),
        'PASSWORD': env('DATABASE_PASSWORD'),
        'HOST': env('DATABASE_HOST'),
        'PORT': env('DATABASE_PORT'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

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
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'



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

I already adjusted the BASE_DIR = Path(__file__).resolve().parent.parent.parent for the settings folder I created.

I am missing something elsewhere.

For clarity, are you saying that these files aren’t found in your local development environment, or in your production deployment environment? Even if in testing, are you testing this with DEBUG=False?

(Management of, and access to static files is usually quite different between a development environment and production, and is affected by the DEBUG setting. See

I’d also question the use of '/' as your MEDIA_URL - that seems like it could cause some problems - but that’s a different issue.)

I should have been more precise, my fault.

For clarity, are you saying that these files aren’t found in your local development environment, or in your production deployment environment? Even if in testing, are you testing this with DEBUG=False?

My problem was in local development mode.
And I did set the DEBUG option wrong. I do not believe that this was the kicker for the static files.
I wanted to use my initial environment variable for local development and set DEBUG=False.
With True the static file work


env = environ.Env(
    # set casting, default value
    DEBUG=(bool, True), <- This was False before
    SECRET_KEY=(str, 'django-insecure-ur5y7+d#j9)j)34wd=sow*c-d=at4i3si5dn590&@p3wz2ynnw'),
    ALLOWED_HOSTS=(list, ['localhost', '127.0.0.1']),
    DATABASE_HOST=(str, 'postgres'),
    DATABASE_USER=(str, 'postgres'),
    DATABASE_PASSWORD=(str, 'postgres'),
    DATABASE_PORT=(int, 9000),
    DATABASE_NAME=(str, 'postgres')

)

Thanks for getting me in the right direction.

Got the static files working in development.

Regarding

'/' as your MEDIA_URL
yes bad idea. did not work.
I have set it up now like this and it works locally:

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

Next thing is getting this to work in prod. But need some reading first.