Functions¶
loader¶
- loader.dump_secrets(fmt='TOML', **kwargs)¶
Dump a secrets dictionary to the specified format.
Dump a secrets dictionary to the specified format, defaulting to TOML.
- Parameters
fmt (string, default="TOML") – The dump format, one of
TOML
,JSON
,YAML
,BespON
, orENV
.kwargs (dict) – A dictionary of configuration variables.
- loader.generate_secret_key()¶
Generate a secret key for a Django app.
Generate a secret key for a Django app, using
django.core.management.utils.get_random_secret_key
.- Returns
A random secret key.
- Return type
string
- loader.load_environment(prefix='DJANGO_ENV_')¶
Load Django configuration variables from the enviroment.
This function searches the environment for variables prepended with
prefix
. Currently, this function only reliably works for string variables, but hopefully will work for other types, dictionaries, and lists in the future.- Parameters
prefix (string, default=”DJANGO_ENV_”) – Prefix for environment variables. This prefix should be prepended to all valid variable names in the environment.
- Returns
A dictionary, possibly empty, of configuration variables and values.
- Return type
dict
- loader.load_file(fn, raise_bad_format=True)¶
Attempt to load configuration variables from
fn
.Attempt to load configuration variables from
fn
. Iffn
does not exist or is not a recognized format, return an empty dict. RaisesImproperlyConfigured
if the file exists and does not match a recognized format unlessraise_bad_format
isFalse
.- Parameters
fn (string) – Filename from which to load configuration values.
raise_bad_format (boolean, default=True) – Determine whether to raise
django.core.exceptions.ImproperlyConfigured
if the file format is not recognized. Default isTrue
.
- Returns
A dictionary, possibly empty, of configuration variables and values.
- Return type
dict
- Raises
django.core.exceptions.ImproperlyConfigured – Raises an
ImproperlyConfigured
exception if the file format is not recognized andraise_bad_format
isTrue
.
- loader.load_secrets(fn='.env', prefix='DJANGO_ENV_', **kwargs)¶
Load a list of configuration variables.
Return a dictionary of configuration variables, as loaded from a configuration file or the environment. Values passed in as
args
or as the value inkwargs
will be used as the configuration variable’s default value if one is not found in the configuration file or environment.- Parameters
fn (string, default=".env") – Configuration filename, defaults to
.env
. May be in TOML, JSON, YAML, or BespON formats. Formats will be attempted in this order.prefix (string, default=”DJANGO_ENV_”) – Prefix for environment variables. This prefix will be prepended to all variable names before searching for them in the environment.
kwargs (dict, optional) – Dictionary with configuration variables as keys and default values as values.
- Returns
A dictionary of configuration variables and their values.
- Return type
dict
- loader.main(argv=None)¶
Run as script, to access
dump()
functions.
- loader.merge(defaults, file, env)¶
Merge configuration from defaults, file, and environment.
- loader.validate_falsy(name, val)¶
Validate that
val
is falsy.Validate that
val
is falsy according to https://docs.python.org/3/library/stdtypes.html#truth-value-testing.- Parameters
val (any) – Configuration variable to validate.
- Returns
True
ifval
is falsy.- Return type
boolean
- Raises
django.core.exceptions.ImproperlyConfigured – Raises an
ImproperlyConfigured
exception on truthy values, with an error message.
- loader.validate_not_empty_string(name, val)¶
Validate that
val
is not an empty string.Validate that
val
is not an empty string.- Parameters
val (any) – Configuration variable to validate.
- Returns
True
ifval
is not an empty string.- Return type
boolean
- Raises
django.core.exceptions.ImproperlyConfigured – Raises an
ImproperlyConfigured
exception on empty strings, with an error message.
- loader.validate_truthy(name, val)¶
Validate that
val
is truthy.Validate that
val
is truthy according to https://docs.python.org/3/library/stdtypes.html#truth-value-testing.- Parameters
val (any) – Configuration variable to validate.
- Returns
True
ifval
is truthy.- Return type
boolean
- Raises
django.core.exceptions.ImproperlyConfigured – Raises an
ImproperlyConfigured
exception on falsy values, with an error message.