I have a django app with the following if else block in my settings.py file
if
block is getting executed when app runs in heroku , else
is getting executed when app is in local system, please explain what is this os.getenviron.get
? and what MBAIR,False
is used for?
import os
if not bool(os.environ.get('MBAIR', False)):
DEBUG = True
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'rsdfmsfgjsdk_sdfhsdfh'
'USER': 'sdfhsdfhsdf',
'PASSWORD': 'sdhsdfhsdhgfsdf',
'HOST': 'localhost',
'PORT': '',
}
}
os.environ.get('MBAIR', False)
searches for an environmental variable called MBAIR. If it does not exist, it returns False.
So when you use that if statement, you're searching for an environmental variable that you're assuming is only set on a heroku server. If found, it sets DEBUG to FALSE and then uses dj_database_url.config to create the database configuration dictionary with the following arguments:
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
Otherwise it uses your manually defined database settings.
edit: you can try this out like this:
>>> import os
>>> os.environ['testvariable'] = "dookie"
>>> os.environ.get('testvariable', False)
'dookie'
>>> os.environ.get('MBAIR', False)
False
>>> os.environ.get('MBAIR', "Hooha")
'Hooha'
Also, False
can be replaced with just about anything.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.