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”, or “ENV” (not currently implemented).

  • kwargs (dict) – A dictionary of configuration variables.

Raises

NotImplementedError – Raises NotImplementedError for format ENV to dump environment 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=False)

Attempt to load configuration variables from fn.

Attempt to load configuration variables from fn. If fn does not exist or is not a recognized format, return an empty dict unless raise_bad_format is True.

Parameters
  • fn (string) – Filename from which to load configuration values.

  • raise_bad_format (boolean, default=False) – Determine whether to raise django.core.exceptions.ImproperlyConfigured if the file format is not recognized. Default is False.

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 and raise_bad_format is True.

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 in kwargs 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()

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 if val 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 if val 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 if val is truthy.

Return type

boolean

Raises

django.core.exceptions.ImproperlyConfigured – Raises an ImproperlyConfigured exception on falsy values, with an error message.