【问题标题】:How do I access imported local settings without a circular import?如何在没有循环导入的情况下访问导入的本地设置?
【发布时间】:2011-11-21 23:19:38
【问题描述】:

在我的 settings.py 文件的末尾,我有:

try:
    from local_settings import *
except ImportError:
    pass

然后我有一个 local_settings.py 文件,其中有一些数据库设置等。在这个文件中,我还想做以下事情(为了使用 django_debug_toolbar):

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

我想把这些设置放在这里,这样它们就不会出现在使用主settings.py文件的生产环境中,但是这个文件当然不能访问原始设置,因为它不知道关于settings.py。如何避免潜在的循环导入以实现我想要做的事情?

【问题讨论】:

    标签: python django django-debug-toolbar


    【解决方案1】:

    我使用的方法是我的设置实际上是一个包而不是一个模块

    设置/ 初始化.py 基础.py local.py #这个在.gitignore上

    初始化.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',)
    

    您可以在 here 中看到这样的示例

    【讨论】:

    • 这是一个不错的方法,但我需要在我的顶级目录中维护一个 settings.py 文件,因为这就是我运行它的 prod 环境需要设置它的方式。
    • @JamieForrest 在您的顶级目录中保留 settings.py 是什么意思?
    【解决方案2】:

    你不能这样做。导入的模块在它自己的范围内执行,并且无法知道它在哪里(以及是否以任何方式导入)。另一种方法是这样的:

    在你的 local_settings 中:

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

    并且在主 settings.py 中

    try:
        import local_settings as local
        has_local = True
    except ImportError:
        has_local = False
    
    # ...
    if has_local:
        MIDDLEWARE_CLASSES += local.MIDDLEWARE_CLASSES
    

    【讨论】:

    • 我喜欢这个想法,但问题是这会将 local_settings.py 中的任何设置都限制在本地。* 所以任何需要访问这些设置的东西都会中断。 (例如,我正在使用 South 进行迁移,它使用 DATABASE['Default']['Engine'] 设置其数据库适配器。如果我按照您所说的进行设置,则在运行服务器时会出现以下错误:There is no South database module 'south.db.None' for your database. Please either choose a supported database, check for SOUTH_DATABASE_ADAPTER[S] settings, or remove South from INSTALLED_APPS. 那是因为它可以t 找到 DATABASES 设置。
    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    相关资源
    最近更新 更多