\$\begingroup\$
\$\endgroup\$
1
I need to inject same variables into settings.py for multiple django apps.hence i wrote a module which takes in output of local() and modifies. This works but is it the right way to do things?
def enable_theme(theme_name, container):
"""
Enable the settings for a custom theme, whose files should be stored
in ENV_ROOT/themes/THEME_NAME (e.g., edx_all/themes/stanford).
The THEME_NAME setting should be configured separately since it can't
be set here (this function closes too early). An idiom for doing this
is:
THEME_NAME = "stanford"
enable_theme(THEME_NAME)
"""
container["THEME_NAME"] = theme_name
container['MITX_FEATURES']['USE_CUSTOM_THEME'] = True
# Calculate the location of the theme's files
theme_root = container['THEME_ROOT'] / "themes" / theme_name
# Include the theme's templates in the template search paths
container['TEMPLATE_DIRS'].append(theme_root / 'templates')
container['THEME_SASS_DIRECTORY'] = theme_root / 'static/saas'
container['MAKO_TEMPLATES']['main'].append(theme_root / 'templates')
# Namespace the theme's static files to 'themes/<theme_name>' to
# avoid collisions with default edX static files
container['STATICFILES_DIRS'].append((u'themes/%s' % theme_name,
theme_root / 'static'))
container['FAVICON_PATH'] = 'themes/%s/images/favicon.ico' % theme_name
#Test case
from theming_utils.template_customizer import enable_theme
THEME_NAME = "sokratik"
PIPELINE = False
MITX_FEATURES['USE_DJANGO_PIPELINE'] = False
DEBUG = True
TEMPLATE_DEBUG = True
enable_theme(THEME_NAME, locals())
himangshujhimangshuj
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
You're not trying to modify a local environment, you're trying to modify the module's dictionary, which you can access by calling globals()
.
Note: the dictionary returned by locals()
isn't meant to be modified.
answered Sep 6, 2013 at 15:33
-
\$\begingroup\$ that explains a bit of stuff, in case I am actually using globals, is it fine to modify globals in this way?? \$\endgroup\$himangshuj– himangshuj2013年09月06日 19:29:34 +00:00Commented Sep 6, 2013 at 19:29
-
\$\begingroup\$ It's okay to modify a module's dictionary, I don't know whether or not it's the right way to handle themes in Django. \$\endgroup\$Changaco– Changaco2013年09月06日 20:07:53 +00:00Commented Sep 6, 2013 at 20:07
lang-py
local()
. Also, you need to format your code so that it shows up correctly. \$\endgroup\$