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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
43Tesseracts
  • 4,617
  • 8
  • 48
  • 94
  • show the complete traceback – joel goldstick Jul 07 '16 at 18:53
  • 1
    Duplicate of http://stackoverflow.com/questions/1626326/how-to-manage-local-vs-production-settings-in-django – rfkortekaas Jul 07 '16 at 19:15
  • 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. – JL Peyret Jul 08 '16 at 07:32
  • 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. – jackotonye Feb 02 '18 at 18:55

3 Answers3

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',
    )
Raddish IoW
  • 302
  • 4
  • 12
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 *
Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
0

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

Murugappan M
  • 18
  • 2
  • 7