Import Django settings from app into project settings?

  django, python, python-3.x, settings

I’ve got a few projects that all use a ‘shared_app’. In that shared_app I have a module named default_settings.py that contains the settings that are used by all projects that have that shared_app.

Folder structure is the usual Django structure:

project_dir
 |
 - project_dir
    |
    - settings.py
    - urls.py
    - wsgi.py
 - shared_app
    |
    - default_settings.py
    - ... all other app files ...
 - other_app
 - some_other_app
 - ...other project files....

In my project_dir/settings.py I have these lines:

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

exec(
    open(os.path.join(BASE_DIR, "shared_app", "default_settings.py")).read()
) in globals()

# Below this line - I can use any settings from default_settings.py 

if DEBUG:
    SESSION_EXPIRE_AT_BROWSER_CLOSE = False

The default_settings.py files contains things like:

DEBUG = is_debug(os.environ.get("DJANGO_DEBUG"))

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

SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY")

SESSION_EXPIRE_AT_BROWSER_CLOSE = True

There are simply too many settings in default_settings.py to have something like from share_app import THIS, THAT, OTHER There are around 20-30 settings.

Is there a better way to do this? Or am I stuck with that slightly confusing, ugly exec() command? Is there anything more ‘pythonic’?

Source: Python-3x Questions

LEAVE A COMMENT