Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I access the __dict__ object for the local scope?
    primarykey
    data
    text
    <p>Here is my situation...</p> <p>I am trying to dynamically generate a bunch of stuff in my settings.py file on a django site.</p> <p>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.</p> <p>for example:</p> <pre><code>from universal_settings import * SITE_NAME = 'First Site' SITE_SLUG = 'firstsite' DEFAULT_FROM_EMAIL = '%s &lt;noreply@otakupride.com&gt;' % SITE_NAME ROOT_URLCONF = 'mysite.urls.%s' % SITE_SLUG TEMPLATE_DIRS += ( os.path.join(PROJECT_ROOT, "templates", SITE_SLUG), ) </code></pre> <p>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</p> <pre><code>from universal_settings import * from utils import get_dynamic_settings SITE_NAME = 'First Site' SITE_SLUG = 'firstsite' get_dynamic_settings( locals() ) </code></pre> <p>And here is the function</p> <pre><code># 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 &lt;noreply@otakupride.com&gt;' % 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']), ) </code></pre> <p>so my question is... how do I add things to the scope of the settings file? it doesn't seem to have a <strong>dict</strong> object available to the variables within it.</p> <p>Maybe I'm going about this all wrong? Thanks for your help!</p> <p>PS - my understanding of the <code>global</code> keyword is that it tells the compiler that the function means to manipulate a global variable <strong>within it's own file</strong> - is there such a thing for the file which the function is called?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload