我 fork 了这个应用程序:https://github.com/cyrilrbt/canadian-nutrient-file/blob/master/cnf/settings.py
您可以在 settings.py
中看到它,下面使用 DEBUG = (os.getenv('CNF_DEBUG', 'True') == 'True')
我在终端中激活了 conda env,并在激活 iPython 控制台后输入命令:
'CNF_DEBUG' in os.environ
这是错误
。
settings.py 的其余部分如下所示:
import os
import datetime
from urllib.parse import urljoin
APP_NAME = "cnf"
APP_SYSTEM_ERROR_SUBJECT_LINE = APP_NAME + "system error"
#Flask Settings
CSRF_ENABLES = True
ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
# looks like a trick to get it to select default True == True
DEBUG = (os.getenv('CNF_DEBUG', 'True') == 'True')
TESTING = (os.getenv('CNF_TESTING', 'True') == 'True')
FLASK_DEBUG = DEBUG
FLASK_BIND = os.getenv('CNF_BIND', 'localhost')
FLASK_PORT = int(os.getenv('CNF_PORT', 8888))
ROOT_URL = 'http://' + FLASK_BIND + ':' + str(FLASK_PORT) + '/'
TEMPLATE_FOLDER = os.path.join(ROOT_PATH, 'cnf', 'templates')
STATIC_FOLDER = os.path.join(ROOT_PATH, 'cnf', 'static')
MONGODB_HOST = os.getenv('CNF_MONGO_HOST', 'localhost')
MONGODB_PORT = int(os.getenv('CNF_MONGO_PORT', 27017))
MONGODB_DB = os.getenv('CNF_MONGO_DB', 'cnf') # name of db
#encryption, cryptographically signs client-side cookies so they cannot be tampered with
#if tampered, session becomes invalid, insecure not to be used production
SECRET_KEY = 'this key is not secure so be careful until it is'
# Flask-User Settings
# from https://flask-user.readthedocs.io/en/latest/configuring_settings.html
USER_APP_NAME = "cnf"
USER_ENABLE_EMAIL = False # register with email
USER_ENABLE_CONFIRM_EMAIL = False # force users to confirm email
USER_ENABLE_USERNAME = True # enable username authentication
USER_REQUIRE_RETYPE_PASSWORD = False # simplify register form
USER_EMAIL_SENDER_NAME = 'nobu'
#USER_EMAIL_SENDER_EMAIL = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91fffef3e4bffaf8fca7a7d1f6fcf0f8fdbff2fefc" rel="noreferrer noopener nofollow">[email protected]</a>' # set up Flask Mail for this
USER_ENABLE_CHANGE_USERNAME = True
USER_ENABLE_CHANGE_PASSWORD = True
USER_ENABLE_FORGOT_PASSWORD = True
#USER_ENABLE_REGISTER = True
USER_ENABLE_REGISTRATION = True
USER_ENABLE_REMEMBER_ME = True
USER_AFTER_LOGIN_ENDPOINT = 'main.member_page'
USER_AFTER_LOGOUT_ENDPOINT = 'main.home_page'
# Flask-Mail settings
# For smtp.gmail.com to work, you MUST set "Allow less secure apps" to ON in Google Accounts.
# Change it in https://myaccount.google.com/security#connectedapps (near the bottom).
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 587
MAIL_USE_SSL = False
MAIL_USE_TLS = True
MAIL_USERNAME = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e688898493c88d8f8bd0d0a6818b878f8ac885898b" rel="noreferrer noopener nofollow">[email protected]</a>'
MAIL_PASSWORD = "..."
#ADMINS = ['"Admin One" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dbbabfb6b2b59bbcb6bab2b7f5b8b4b6" rel="noreferrer noopener nofollow">[email protected]</a>>,]
# Sendgrid settings
#SENDGRID_API_KEY='place-your-sendgrid-api-key-here'
正如上面评论中所指出的,我认为这可能只是一个技巧,而不是使用评论,但是这样做还有什么其他原因吗?在哪里可以找到 CNF_TESTING
和 CNF_DEBUG
?
它还对 ROOT_URL
的 FLASK_BIND
和 FLASK_PORT
执行此操作。
最佳答案
这些是环境变量。
编写环境变量有很多优点,其中包括更容易的容器设置、更容易和标准化的配置访问(甚至在程序之间)以及更好的扩展。
有关环境变量的更多信息,请参阅 Wikipedia article或12-factor根据@flakes的建议。
关于python - 为什么要在 Python Flask 应用程序的 settings.py 中使用 os.getenv() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63758154/