Separating Django installed apps between Development vs Production
Asked Answered
S

3

10

I have 3 settings files:

  • base.py (shared)
  • development.py
  • production.py

base.py has:

INSTALLED_APPS = (

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes'
    ...

but I have some apps that I only want in my development environment, for example, debug-toolbar.

I tried this in development.py:

INSTALLED_APPS += (
    'debug_toolbar',
)

But get the error: NameError: name 'INSTALLED_APPS' is not defined

The settings files are connected like this:

__init__.py

from .base import *

try:
    from .production import *
except:
    from .development import *

How can I differentiate the installed apps between my production/development environment?

Serrulate answered 7/7, 2016 at 18:39 Comment(4)
show the complete tracebackChiaroscuro
Duplicate of #1626826Sandbox
Dev doesn't "see" base in your case. Your code needs to be in init. Or reorganized. But an imported module (dev) doesn't inherit the namespace from the importing module (init) that happened to bring in the installed apps from base. So... Installed apps is nowhere to be seen. Sorry for caps and underscore mistypes (on tablet keyboard) but that's the core issue here.Alumnus
You don't need package level import when you need to access the settings for base or production DJANGO_SETTINGS_MODULE=project_name.settings.production and in the production.py from .base import * and override any setting that need to be changed.Mullen
S
7

I simply test for DEBUG in my settings.py (assuming that in production DEBUG == FALSE) and add the apps thus:

# settings.py
if DEBUG:
    INSTALLED_APPS += (
        # Dev extensions
        'django_extensions',
        'debug_toolbar',
    )
Suzy answered 11/10, 2019 at 10:28 Comment(1)
Why are you using a tuple instead of a list?Suckling
H
5

I dealt with this issue myself, i hacked it like this:

base.py (mine was settings.py)

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes'
    ... )


# rest of settings.py variables ...

def _add_installed_app(app_name):
    global INSTALLED_APPS

    installed_apps = list(INSTALLED_APPS)
    installed_apps.append(app_name)
    INSTALLED_APPS = tuple(installed_apps)

ADD_INSTALLED_APP = _add_installed_app

development.py (mine was settings_debug.py)

from base import *

ADD_INSTALLED_APP('debug_toolbar')

production.py

from base import *
Helio answered 7/7, 2016 at 18:53 Comment(0)
R
0

use extend to append one list to another and state installed apps as a list (square brackets []) instead of tuple (())

Reinke answered 23/7, 2021 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.