Skip to main content

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.

[tool.django]

# Paths are relative to the TOML file (unless they are absolute)
BASE_DIR = { $path = "." }
STATIC_ROOT = { $path = "staticfiles" }

# This sets the key based on the environment variable
SECRET_KEY = { $env = "SECRET_KEY" }

# This sets the key based on the environment variable, but has a fallback
ADMIN_URL_PATH = { $env = "ADMIN_URL_PATH", default="admin" }

# Booleans, arrays, tables (dictionaries), integers, strings, floats, dates are all supported in TOML
DEBUG = true
ALLOWED_HOSTS = [
  "127.0.0.1",
]

# This is an implicit dictionary and equivalent to `COLTRANE = { TITLE = "Example blog" }`
[tool.django.COLTRANE]
TITLE = "Example blog"

# Any name can be used under `apps` for organizational purposes
[tool.django.apps.tailwind-cli]
TAILWIND_CLI_USE_DAISY_UI = true
TAILWIND_CLI_SRC_CSS = ".django_tailwind_cli/source.css"

# These settings are included when the `ENVIRONMENT` environment variable is "development"
[tool.django.envs.development]
ALLOWED_HOSTS = { $insert = "example.localhost" }

# These settings are included when the `ENVIRONMENT` environment variable is "production"
[tool.django.envs.production]
DEBUG = false
ALLOWED_HOSTS = { $insert = "example.com" }

Features 🤩

Variables

Use ${SOME_VARIABLE_NAME} to use an existing setting as a value.

[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].

[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].

[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 😎

By default, special operations are denoted by an inline table, (aka a dictionary) with a key that starts with a $.

The prefix and suffix that denotes a special operation can be configured with TOML_SETTINGS_SPECIAL_PREFIX or TOML_SETTINGS_SPECIAL_SUFFIX in [tool.django].

[tool.django]
TOML_SETTINGS_SPECIAL_PREFIX = "&"
TOML_SETTINGS_SPECIAL_SUFFIX = "*"
BASE_DIR = { &path* = "." }

Path

Converts a string to a Path object by using a $path key. Handles relative paths based on the location of the parsed TOML file.

[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.

[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.

[tool.django]
ALLOWED_HOSTS = { $insert = "127.0.0.1" }

Example Integrations 💚

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

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))

...

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.

  1. pyproject.toml -> [tool.django]
  2. pyproject.toml -> [tool.django.apps.*]
  3. pyproject.toml -> [tool.django.envs.*] that match ENVIRONMENT environment variable
  4. django.toml -> [tool.django]
  5. django.toml -> [tool.django.apps.*]
  6. django.toml -> [tool.django.envs.*] that match ENVIRONMENT environment 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

dj_toml_settings-0.3.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_toml_settings-0.3.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file dj_toml_settings-0.3.0.tar.gz.

File metadata

  • Download URL: dj_toml_settings-0.3.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dj_toml_settings-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5f7744b80ae47ea681c18491c37b8fc1fce72263c769509e6ad6f65a77d563f1
MD5 1c63e9f5304fecf243d928d6f23249bb
BLAKE2b-256 d4d6180f47a1895eb1c102262eb836aeb65253206e59a5a9111a1ac568c13fe1

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_toml_settings-0.3.0.tar.gz:

Publisher: publish.yml on adamghill/dj-toml-settings

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dj_toml_settings-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dj_toml_settings-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d85361fbeecbb134383c1f36b52f36f5f155ac7e535c8e4f18d32d529eb6a269
MD5 d391a67ad2a7bd19212c4162a4fc1438
BLAKE2b-256 296c5f2c992c8f8200af5801205625653f0b280ece868480734469f6537c1b0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_toml_settings-0.3.0-py3-none-any.whl:

Publisher: publish.yml on adamghill/dj-toml-settings

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page