Skip to content

Instantly share code, notes, and snippets.

@lyhapple
Last active November 28, 2017 03:29
Show Gist options
  • Save lyhapple/6ae476eed86bd5d345fa1dc793062bde to your computer and use it in GitHub Desktop.
Save lyhapple/6ae476eed86bd5d345fa1dc793062bde to your computer and use it in GitHub Desktop.
django自动导入app自定义配置
import os
import importlib
APP_ROOT = os.path.join(BASE_DIR, 'app')
APP_DIR_LIST = os.listdir(APP_ROOT)
APPS = {} # {'account': '/var/test/account'}
for app_name in APP_DIR_LIST:
if app_name == 'tpl':
continue
app = os.path.join(APP_ROOT, app_name)
if os.path.isdir(app):
APPS[app_name] = app
for app_name, app_dir in APPS.iteritems():
setting_file = os.path.join(app_dir, 'settings.py')
if os.path.exists(setting_file):
app_module = importlib.import_module('%s.settings' % app_name)
setting_all = app_module.__dict__
attrs = dir(app_module)
for attr in attrs:
if attr.startswith('__'):
continue
globals().update({attr: setting_all.get(attr)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment