Here is my situation...
I am trying to dynamically generate a bunch of stuff in my settings.py file on a django site.
I am setting up several sites, (via sites framework) and I want to have some values I plug in to a function that will generate a portion of the settings file for each site.
for example:
from universal_settings import *
SITE_NAME = 'First Site'
SITE_SLUG = 'firstsite'
DEFAULT_FROM_EMAIL = '%s <[email protected]>' % SITE_NAME
ROOT_URLCONF = 'mysite.urls.%s' % SITE_SLUG
TEMPLATE_DIRS += ( os.path.join(PROJECT_ROOT, "templates", SITE_SLUG), )
obviously it's a huge violation of DRY to have those last 3 lines in the settings file for every site running this code. So I want to do something like this
from universal_settings import *
from utils import get_dynamic_settings
SITE_NAME = 'First Site'
SITE_SLUG = 'firstsite'
get_dynamic_settings( locals() )
And here is the function
# WARNING: THIS CODE DOES NOT WORK!
def get_dynamic_settings(context_dict):
global DEFAULT_FROM_EMAIL
global ROOT_URLCONF
global TEMPLATE_DIRS
DEFAULT_FROM_EMAIL = '%s <[email protected]>' % context_dict['SITE_NAME']
ROOT_URLCONF = 'mysite.urls.%s' % context_dict['SITE_SLUG']
TEMPLATE_DIRS += ( os.path.join(PROJECT_ROOT, "templates", context_dict['SITE_SLUG']), )
so my question is... how do I add things to the scope of the settings file? it doesn't seem to have a dict object available to the variables within it.
Maybe I'm going about this all wrong? Thanks for your help!
PS - my understanding of the global
keyword is that it tells the compiler that the function means to manipulate a global variable within it's own file - is there such a thing for the file which the function is called?