我对 Django 有一定的经验,并且正在使用现在推荐的“多个设置文件”模式设置一个新项目。每个 settings.py 文件都会导入一个基本的 settings.py,然后覆盖特定的设置。每个暂存环境(dev、qa、prod)都有一个文件。启动 Django 进程时,我确保将设置标志设置为适当的 settings.py 文件,如下所示
manage.py runserver --settings=myproj.settings.dev
或者
manage.py runfcgi --settings=myproj.settings.prod method=threaded daem...[more flags]
我的问题是,如何将特定于环境的常量放入视图的函数中。我的项目有一些特定的常量(curl cert/host/port),每个环境都不同。目前我只知道如何在导入路径中包含环境,但这对我不起作用,如果有人可以请帮助那将是很棒的。
这是一个示例 views.py 文件,它应该有助于使我的问题更加清晰。
# A sample Django view.py file
from django.template.response import TemplateResponse
from myproj import settings
def index(request):
# these assignments work, but I have to add conditional logic to pick the correct
# value, I would prefer not to do this.
dev_curl_host = settings.dev.CONNECT['host']
qa_curl_host = settings.qa.CONNECT['host']
prod_curl_host = settings.prod.CONNECT['host']
# I want to do something like this, where the settings import get assigned the
# correct values for the staging environment.
# It seems like Django is already doing this with settings like Debug, how?
curl_host = settings.CONNECT['host']