Search code examples
pythondjangodjango-debug-toolbar

How do I access imported local settings without a circular import?


At the end of my settings.py file, I have:

try:
    from local_settings import *
except ImportError:
    pass

I then have a local_settings.py file where I have some db settings, etc. In this file I would also like to do the following (in order to use django_debug_toolbar):

INTERNAL_IPS = ('127.0.0.1',)
MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)
INSTALLED_APPS += ('debug_toolbar',)

I want to put these settings here so that they don't show up in the production environment, which uses the main settings.py file, but of course this file can't access the original settings because it doesn't know about settings.py. How can I avoid a potentially circular import to achieve what I'm trying to do?


Solution

  • I use an approach in which my settings is actually a package and not a module

    settings/ init.py base.py local.py #this one is on .gitignore

    init.py:

    from setings import *
    try:
        from local.py import *
    except ImportError:
        pass
    

    base.py:

    import os
    DEBUG = False
    TEMPLATE_DEBUG = DEBUG
    
    SITE_ROOT = os.path.join( os.path.dirname( os.path.realpath(__file__) ) ,'..' )
    
    ADMINS = (
        # ('Your Name', 'your_email@example.com'),
    )
    
    MANAGERS = ADMINS
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': '',                      # Or path to database file if using sqlite3.
            'USER': '',                      # Not used with sqlite3.
            'PASSWORD': '',                  # Not used with sqlite3.
            'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        }
        }
    
    etc...
    

    local.py:

    from settings import settings as PROJECT_DEFAULT
    
    PREPEND_WWW = False
    DEBUG = True
    TEMPLATE_DEBUG = DEBUG
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_pyscopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'somesecretname',                      # Or path to database file if using sqlite3.
            'USER': 'somesecretuser',                      # Not used with sqlite3.
            'PASSWORD': 'somesecretpassword',                  # Not used with sqlite3.
            'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        }
    }
    
    INSTALLED_APPS += PROJECT_DEFAULT.INSTALLED_APPS + ('debug_toolbar',)
    

    You can see an example of this in here