Mewtwo
Mewtwo

Reputation: 1311

Unable to import some variables from django settings

I have set the DJANGO_SETTINGS_MODULE to be my settings file. Some sample lines of this settings file follow:

TEST = {
    'CLIENT': os.environ.get("CLIENT_NAME", "unknown"),
    'ENVIRONMENT': os.environ.get("ENVIRONMENT", "unknown")
}

client = os.environ.get("CLIENT_NAME", "unknown")
environment = os.environ.get("ENVIRONMENT", "unknown")

I then try to import the django settings by using

from django.conf import settings as django_settings

I am then able to print the value of the django_settings.TEST['CLIENT'] but not of the django_settings.client as I get 'Settings' object has no attribute 'client'

What am I missing? To my the only difference is that TEST is a dict while client is a variable of string type.

Upvotes: 0

Views: 533

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599520

It is indeed because settings must be in all capitals. This is specified in the settings documentation:

There’s nothing stopping you from creating your own settings, for your own Django apps. Just follow these guidelines:

  • Setting names must be all uppercase.

Behind the scenes, this is because django.conf.settings is not actually a module but a class that is dynamically created based on that module; the code only reads variables that are in all caps.

(Note also that stylistically, this makes sense; settings by definition are constants, and Python style is to put constants in all caps.)

Upvotes: 1

Related Questions