一尘不染

Django本地设置

django

我正在尝试在Django 1.2中使用local_setting ,但对我来说不起作用。目前,我只是将local_settings.py添加到我的项目中。

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'banco1',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '123',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

local_settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'banco2',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '123',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

问题在于local_settings.py不会覆盖settings.py。怎么了?


阅读 424

收藏
2020-03-27

共1个答案

一尘不染

你不仅可以添加local_settings.py,还必须显式导入它。

在最后一刻你的settings.py的,补充一点:

try:
    from local_settings import *
except ImportError:
    pass

try / except块在那里,因此当你实际上尚未定义local_settings文件时,Python只会忽略这种情况。

2020-03-27