Question

I am moderatly experienced with Django and am setting up a new project using the now recommended "multiple settings files" pattern. Each settings.py file will import a base settings.py and then override specific setting. There will be one file for each staging environment (dev, qa, prod). When starting the Django process, I am making sure to set the settings flag to the appropriate settings.py file like so

manage.py runserver --settings=myproj.settings.dev

or

manage.py runfcgi --settings=myproj.settings.prod method=threaded daem...[more flags]

My question is, how do I get an environment specific constant into my view's functions. I have some specific constants (curl cert/host/port) for my project that are different for each environment. Currently I have only figured out how to include the environment in the import path, but this won't work for me, if someone can please help that would be awesome.

Here is an example views.py file that should help make my question a little clearer.

# 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']
Était-ce utile?

La solution

Instead of

from myproj import settings

do

from django.conf import settings

That's why DEBUG works:

https://docs.djangoproject.com/en/dev/topics/settings/#using-settings-in-python-code

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top