多环境下,配置django settings

  1. 在settings.py旁边创建settings文件夹
  2. 重命名settings.py为base.py,并移动到新建的settings文件夹中
  3. 在settings/ 文件夹创建其它的配置文件
    以下为Django 2 by example原文
    In real-world projects you will have to deal with multiple
    environments. You will have at least a local and a production
    environment, but you could have other environments as well,
    such as testing or pre-production environments. Some project
    settings will be common to all environments, but others will
    have to be overridden per environment. Let's set up project
    settings for multiple environments while keeping everything
    neatly organized.
    Create a settings/ directory next to the settings.py file of the educa
    project. Rename the settings.py file to base.py and move it into
    the new settings/ directory. Create the following additional files
    inside the setting/ folder so that the new directory looks as
    follows:


    image.png

    These files are as follows:
    base.py: The base settings file that contains common
    settings (previously settings.py)
    local.py: Custom settings for your local environment
    pro.py: Custom settings for the production environment
    Edit the settings/base.py file and replace the following line:

BASE_DIR =
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

With the following one:

BASE_DIR =
os.path.dirname(os.path.dirname(os.path.abspath(os.path.join(__f
ile__, os.pardir))))

We have moved our settings files to a directory one level
lower, so we need BASE_DIR to point to the parent directory to be
correct. We achieve this by pointing to the parent directory
with os.pardir.
Edit the settings/local.py file and add the following lines of code:

from .base import *
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

This is the settings file for our local environment. We import
all settings defined in the base.py file and we only define specific
settings for this environment. We have copied the DEBUG and
DATABASES settings from the base.py file, since these will be set per
environment. You can remove the DATABASES and DEBUG settings
from the base.py settings file.
Edit the settings/pro.py file and make it look as follows:

from .base import *
DEBUG = False
ADMINS = (
('Antonio M', 'email@mydomain.com'),
)
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
}
}

These are the settings for the production environment. Let's
take a closer look at each of them:
DEBUG: Setting DEBUG to False should be mandatory for any
production environment. Failing to do so will result in
traceback information and sensitive configuration data
exposed to everyone.
ADMINS: When DEBUG is False and a view raises an exception,
all information will be sent by email to the people
listed in the ADMINS setting. Make sure to replace the
name/email tuple with your own information.
ALLOWED_HOSTS: Django will only allow the hosts included
in this list to serve the application. This is a security
measure. We include the asterisk symbol * to refer to
all hostnames. We will limit the hostnames that can be
used for serving the application later.
DATABASES: We just keep this setting empty. We are going
to cover database setup for production hereafter.

When handling multiple environments, create a base settings file and a
settings file for each environment. Environment settings files should inherit
the common settings and override environment-specific settings.
We have placed the project settings in a different location than
the default settings.py file. You will not be able to execute any
commands with the manage.py tool unless you specify the
settings module to use. You will need to add a --settings flag
when you run management commands from the shell or set a
DJANGO_SETTINGS_MODULE environment variable.
Open the shell and run the following command:

export DJANGO_SETTINGS_MODULE=educa.settings.pro

This will set the DJANGO_SETTINGS_MODULE environment variable for
the current shell session. If you want to avoid executing this
command for each new shell, add this command to your
shell's configuration in the .bashrc or .bash_profile files. If you
don't set this variable you will have to run management
commands, including the --settings flag, as follows:

python manage.py migrate --settings=educa.settings.pro

You have successfully organized settings for handling multiple
environments.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,106评论 0 10
  • 在阳光明媚的夜晚,我坐在屋檐下抬着头,看着漂亮发光的月亮,我在和月亮说话:“我:月亮啊!月亮啊!如果我和你这么漂亮...
    我爱夜晚阅读 201评论 0 1
  • 中年是一杯下午茶,有人说他读到这句话的时候才不到30岁,而我又何尝不是呢!当然在我还没到中年的时候确实也没有读懂为...
    枕前发尽千般愿阅读 275评论 1 2
  • (双三四号)申请入驻搜狐公众平台 一、日常经历话是敲门砖。以前我的同事去向清洁阿姨借拖巴,说也奇怪,阿姨就是不借给...
    去年的三月七阅读 1,727评论 6 12
  • 班里有十几个孩子是住宿生。每个星期家长送来时,都会给孩子留一些零花钱,多的十来块,少的两三块。今天上课写作业时,有...
    刘乐丹的阅读 154评论 1 0