简体   繁体   English

Heroku和本地部署中的Django应用

[英]Django app in heroku and local deployment

I have a django app with the following if else block in my settings.py file 我的settings.py文件中有一个django应用程序,其中包含以下内容

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 ? if在heroku中运行app时执行块, else在本地系统中执行时执行块,请说明os.getenviron.get什么? and what MBAIR,False is used for? 以及MBAIR,False用于什么?

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. os.environ.get('MBAIR', False)搜索称为MBAIR的环境变量。 If it does not exist, it returns False. 如果不存在,则返回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语句时,您正在搜索假设仅在heroku服务器上设置的环境变量。 If found, it sets DEBUG to FALSE and then uses dj_database_url.config to create the database configuration dictionary with the following arguments: 如果找到,它将DEBUG设置为FALSE,然后使用dj_database_url.config创建具有以下参数的数据库配置字典:

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. 此外, False几乎可以替换为任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM