Load Django settings from a TOML file
Project description
dj-toml-settings
Load Django settings from a TOML file
dj-toml-settings reads settings from a TOML file. By default, both pyproject.toml and django.toml files are parsed for settings in the [tool.django] namespace.
# pyproject.toml
[tool.django]
ALLOWED_HOSTS = [
"127.0.0.1",
]
Features
Variables
Use ${SOME_VARIABLE_NAME} to use an existing setting as a value.
# pyproject.toml
[tool.django]
GOOD_IPS = ["127.0.0.1"]
ALLOWED_HOSTS = ${GOOD_IPS}
Apps
[tool.django.apps.{ANY_NAME_HERE}] sections of the TOML file can be used to group settings together. They can be named anything. They will override any settings in [tool.django].
# pyproject.toml
[tool.django]
ALLOWED_HOSTS = ["127.0.0.1"]
[tool.django.apps.tailwind-cli]
TAILWIND_CLI_USE_DAISY_UI = true
TAILWIND_CLI_SRC_CSS = ".django_tailwind_cli/source.css"
Environments
The [tool.django.envs.{ENVIRONMENT_NAME}] section of the TOML file will be used when {ENVIRONMENT_NAME} is set to the ENVIRONMENT environment variable. For example, ENVIRONMENT=production python manage.py runserver will load all settings in the [tool.django.envs.production] section. There settings will override any settings in [tool.django.apps.*] or [tool.django].
# pyproject.toml
[tool.django]
ALLOWED_HOSTS = ["127.0.0.1"]
[tool.django.envs.development]
ALLOWED_HOSTS = ["example.localhost"]
[tool.django.envs.production]
ALLOWED_HOSTS = ["example.com"]
Special operations
Path
Convert a string to a Path object. Handles relative paths based on the TOML file.
# pyproject.toml
[tool.django]
BASE_DIR = { path = "." }
PROJECT_DIR = { path = "./your_project_folder" }
REPOSITORY_DIR = { path = "./.." }
Environment Variable
Retrieve variables from the environment by using an env key. Specify an optional default key for a fallback value.
# pyproject.toml
[tool.django]
EMAIL_HOST_PASSWORD = { env = 'SECRET_PASSWORD' }
SECRET_KEY = { env = 'SECRET_KEY', default = 'this-is-a-secret' }
Arrays
Add items to an array by using the insert key.
# pyproject.toml
[tool.django]
ALLOWED_HOSTS = { insert = '127.0.0.1' }
Integration with Django
This will override any variables defined in settings.py with settings from the TOML files.
# settings.py
from pathlib import Path
from dj_toml_settings import configure_toml_settings
BASE_DIR = Path(__file__).resolve().parent.parent
...
configure_toml_settings(base_dir=BASE_DIR, data=globals())
Integration with nanodjango
# app.py
from pathlib import Path
from dj_toml_settings import get_toml_settings
base_dir = Path(__file__).resolve().parent
app = Django(**get_toml_settings(base_dir=base_dir))
...
Integration with coltrane
# app.py
from pathlib import Path
from django.core.management import execute_from_command_line
from dj_toml_settings import get_toml_settings
from coltrane import initialize
base_dir = Path(__file__).resolve().parent.parent
wsgi = initialize(**get_toml_settings(base_dir=base_dir))
if __name__ == "__main__":
execute_from_command_line()
...
Precedence
This is the order that files and sections are parsed (by default). The later sections override the previous settings.
pyproject.toml->[tool.django]pyproject.toml->[tool.django.apps.*]pyproject.toml->[tool.django.envs.*]that matchENVIRONMENTenvironment variabledjango.toml->[tool.django]django.toml->[tool.django.apps.*]django.toml->[tool.django.envs.*]that matchENVIRONMENTenvironment variable
Specify a TOML file
from pathlib import Path
from dj_toml_settings import get_toml_settings
base_dir = Path(__file__).resolve().parent
toml_settings = get_toml_settings(base_dir=base_dir, toml_settings_files=["custom-settings.toml"])
...
Test
uv install pip install -e .[dev]just test
Inspiration
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dj_toml_settings-0.1.0.tar.gz.
File metadata
- Download URL: dj_toml_settings-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3373421fd6457ca78423bc918484f2f60f0a8431842df18da766da500de71a32
|
|
| MD5 |
58e170851f86626a878f0c4c59ad93af
|
|
| BLAKE2b-256 |
551ecdef6cd9d526d1338335c60f6e08015bad66ea128df68462de84960f76be
|
Provenance
The following attestation bundles were made for dj_toml_settings-0.1.0.tar.gz:
Publisher:
publish.yml on adamghill/dj-toml-settings
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_toml_settings-0.1.0.tar.gz -
Subject digest:
3373421fd6457ca78423bc918484f2f60f0a8431842df18da766da500de71a32 - Sigstore transparency entry: 438937980
- Sigstore integration time:
-
Permalink:
adamghill/dj-toml-settings@96efb96b6ba9f1aa3e14c08bea42a1f484d79fac -
Branch / Tag:
refs/heads/main - Owner: https://github.com/adamghill
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@96efb96b6ba9f1aa3e14c08bea42a1f484d79fac -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file dj_toml_settings-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dj_toml_settings-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7530469307339f528dd6221126557c3a5b3768d6825bacdeec74c28007b21d4
|
|
| MD5 |
d2bd6c931f84ab4475777f15f2689748
|
|
| BLAKE2b-256 |
0742ecfac54a32def26fa9a709db70192984700696de9a2a3529562db2f175af
|
Provenance
The following attestation bundles were made for dj_toml_settings-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on adamghill/dj-toml-settings
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_toml_settings-0.1.0-py3-none-any.whl -
Subject digest:
b7530469307339f528dd6221126557c3a5b3768d6825bacdeec74c28007b21d4 - Sigstore transparency entry: 438938001
- Sigstore integration time:
-
Permalink:
adamghill/dj-toml-settings@96efb96b6ba9f1aa3e14c08bea42a1f484d79fac -
Branch / Tag:
refs/heads/main - Owner: https://github.com/adamghill
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@96efb96b6ba9f1aa3e14c08bea42a1f484d79fac -
Trigger Event:
workflow_dispatch
-
Statement type: