Package: django-configvars
Django ConvigvarsConfigure your Django project in easy and readable way.DescriptionConfigvars gives possibility to configure Django-based project with local file and environment variables (i.e. for Docker containers).Environmental variables are the most important. If not set, the variables from thelocalmodule will be used, or if these are not present either - the default values will be used:ENV > LOCAL > DEFAULTInstallationpip install git+https://gitlab.com/marcinjn/django-configvars.gitBasic configurationAddconfigvarsto yoursettings.INSTALLED_APPS:INSTALLED_APPS=[# ..."configvars",# ...]QuickstartIn yoursettings.pyadd these lines at the top of the file:fromconvigvarsimportconfig,secretSOME_API_KEY=config("SOME_API_KEY","default_api_key")SOME_API_SECRET=secret("SOME_API_SECRET","")Then use local settings to set these values or pass them by environment
variables. To use local file, add these settings tolocal.pyfile in
the same folder wheresettings.pyfile is located, and fill it with:SOME_API_KEY = "NEW_API_KEY"
SOME_API_SECRET = "NEW_API_SECRET"To check if they are apllied propely runmanage.py configvars.You can override these settings by using environment vars (i.e. for
deployment in containers). To do so just declare an environment variable
as usual:SOME_API_KEY="ENV_API_KEY" manage.py configvarsIn case of secrets, you should provide a path to the secret file
containing a value:SOME_API_SECRET="/run/secrets/SOME_API_SECRET" manage.py configvarsIf file does not exist, the path will be interpreted as typical string value.UsageConfig vars declarationIn yoursettings.pyfile declare configurable variables by usingconfigorsecretfunctions. The first one is used for regular variables, the second one - for secure variables (like passwords, secrets, etc).DATABASES={"default":{"NAME":config("DB_NAME","example"),# `example` as default database name"USER":config("DB_USER","postgres"),# `postgres` as default username"PASSWORD":secret("DB_PASSWORD"),"HOST":config("DB_HOST","localhost"),# `localhost` as default host"PORT":config("DB_PORT",5432),# `5432` as default port}}Show configurable variables for your projectpythonmanage.pyconfigvarsShould result in something like that:DB_NAME = 'example'
DB_USER = 'postgres'
DB_PASSWORD = None
DB_HOST = 'localhost'
DB_PORT = 5432Show only changed config variablesTo show changed config variables bylocal.pyor environment variables use:pythonmanage.pyconfigvars--changedAdding short description to your config variablesIn yoursettings.pydeclareconfigorsecretwith additionaldescargument:MY_CUSTOM_VARIABLE=config("MY_CUSTOM_VARIABLE","default_value",desc="Set's custom variable")Then you can dump your config variables with descriptions:$pythonmanage.pyconfigvars--commentMY_CUSTOM_VARIABLE='default_value'# Set's custom variableLocal settingsDjango Configvars will try to import<projectname>.localmodule by
default. By using this file you can customize your config variables -
they will be used as current values.To do so, create emptylocal.pyin directory where yoursettings.pyfile
is located, then assign values to your variables.As local config variables are specific to a local machine, consider addinglocal.pyto.gitignore.Note that:Local settings can be overriden by environment variablesLocal settings can be skipped for your projectTo change location or name of your local settings file, you must
initialize Django Configvars explicitely insettings.pymodule:from configvars import initialize

initialize("other.location.of.settings_local")Environment variablesDjango Config vars will check at the first whether environment name of
the variable is defined. It is important for deployments in containers,
where configuration variables are passed mostly by environment variables.If environment variable does not exist, a local variable will be
used. If local value is not defined, a default value will be used.Environment variables can be prefixed to solve issues with eventual name
conflicts. To do so you must initialize Django Configvars explicitely insettings.pyfile:from configvars import initialize

initialize(env_prefix="MYPREFIX_")SupportTo ask question please create an issue.To dobetter support for type castsconfig vars view for Django AdminContributingYou can contribute by creating issues, feature requests or merge requests.Authors and acknowledgmentMarcin NowakLicenseISC LicenseCopyright (c) 2023 Marcin NowakPermission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.