Reference Django settings from your code
April 27, 2021 ‐ 1 min read
Accessing your application settings in Django can be useful for several reasons. You may want to check whether you are running in debug mode or you may want to access some keys specific to your current environment. To access your settings you import a settings
object from django.conf
, instead of importing the settings
module itself.
Note that django.conf.settings
is an object and not a python module. Therefore you cannot import individual settings but you should access them via the settings object. This is shown in the example below.
from django.conf import settings
credentials.update({
'STRIPE_PUBLIC_KEY': settings.STRIPE_PUBLIC_KEY
})
In case you are defining your own settings, which you are free to do, make sure to define them using an all uppercase name.
The settings object is only for reading settings, not for changing them. The only place where you should change your settings is in your Django settings module.
from django.conf import settings
# PLEASE DON'T
settings.STRIPE_PUBLIC_KEY = ''