dj-toml-settings


Namedj-toml-settings JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryLoad Django settings from a TOML file
upload_time2025-08-28 02:17:38
maintainerNone
docs_urlNone
authorAdam Hill
requires_python>=3.10
licenseThe MIT License (MIT) Copyright (c) 2020 Ceterai Copyright (c) 2025 adamghill Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords django web toml
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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.

```toml
[tool.django]
BASE_DIR = { path = "." }
SECRET_KEY = { env = "SECRET_KEY" }
ADMIN_URL_PATH = { env = "ADMIN_URL_PATH", default="admin" }
DEBUG = true
ALLOWED_HOSTS = [
  "127.0.0.1",
]

# Implicit dictionaries are supported
# This is equivalent to `COLTRANE = { TITLE = "Example blog" }`
[tool.django.COLTRANE]
TITLE = "Example blog"

# Any app name can be used 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 `ENVIRONMENT` environment variable is "development"
[tool.django.envs.development]
DEBUG = false
ALLOWED_HOSTS = [
  "example.localhost",
]

# These settings are included when `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.

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

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

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

Converts a string to a `Path` object. Handles relative paths based on the location of TOML file.

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

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

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

```python
# 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](https://nanodjango.readthedocs.io) 

```python
# 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](https://coltrane.adamghill.com)

```python
# 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 ๐Ÿค“

```python
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 ๐Ÿ˜

- [django-pyproject](https://github.com/Ceterai/django-pyproject)
- [django-settings-toml](https://github.com/maxking/django-settings-toml)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "dj-toml-settings",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "django, web, toml",
    "author": "Adam Hill",
    "author_email": "Adam Hill <adam@adamghill.com>",
    "download_url": "https://files.pythonhosted.org/packages/ac/8c/1b3abbf718b2358d13df00e204ec5272a27f983ed48e9b9c3d774e10ef07/dj_toml_settings-0.2.0.tar.gz",
    "platform": null,
    "description": "# dj-toml-settings \u2699\ufe0f\n\n> Load Django settings from a TOML file\n\n`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.\n\n```toml\n[tool.django]\nBASE_DIR = { path = \".\" }\nSECRET_KEY = { env = \"SECRET_KEY\" }\nADMIN_URL_PATH = { env = \"ADMIN_URL_PATH\", default=\"admin\" }\nDEBUG = true\nALLOWED_HOSTS = [\n  \"127.0.0.1\",\n]\n\n# Implicit dictionaries are supported\n# This is equivalent to `COLTRANE = { TITLE = \"Example blog\" }`\n[tool.django.COLTRANE]\nTITLE = \"Example blog\"\n\n# Any app name can be used for organizational purposes\n[tool.django.apps.tailwind-cli]\nTAILWIND_CLI_USE_DAISY_UI = true\nTAILWIND_CLI_SRC_CSS = \".django_tailwind_cli/source.css\"\n\n# These settings are included when `ENVIRONMENT` environment variable is \"development\"\n[tool.django.envs.development]\nDEBUG = false\nALLOWED_HOSTS = [\n  \"example.localhost\",\n]\n\n# These settings are included when `ENVIRONMENT` environment variable is \"production\"\n[tool.django.envs.production]\nDEBUG = false\nALLOWED_HOSTS = { insert = \"example.com\" }\n```\n\n## Features \ud83e\udd29\n\n### Variables\n\nUse `${SOME_VARIABLE_NAME}` to use an existing setting as a value.\n\n```toml\n[tool.django]\nGOOD_IPS = [\"127.0.0.1\"]\nALLOWED_HOSTS = ${GOOD_IPS}\n```\n\n### Apps\n\n`[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]`.\n\n```toml\n[tool.django.apps.tailwind-cli]\nTAILWIND_CLI_USE_DAISY_UI = true\nTAILWIND_CLI_SRC_CSS = \".django_tailwind_cli/source.css\"\n```\n\n### Environments\n\nThe `[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]`.\n\n```toml\n[tool.django]\nALLOWED_HOSTS = [\"127.0.0.1\"]\n\n[tool.django.envs.development]\nALLOWED_HOSTS = [\"example.localhost\"]\n\n[tool.django.envs.production]\nALLOWED_HOSTS = [\"example.com\"]\n```\n\n## Special operations \ud83d\ude0e\n\n### Path\n\nConverts a string to a `Path` object. Handles relative paths based on the location of TOML file.\n\n```toml\n[tool.django]\nBASE_DIR = { path = \".\" }\nPROJECT_DIR = { path = \"./your_project_folder\" }\nREPOSITORY_DIR = { path = \"./..\" }\n```\n\n### Environment Variable\n\nRetrieve variables from the environment by using an `env` key. Specify an optional `default` key for a fallback value.\n\n```toml\n[tool.django]\nEMAIL_HOST_PASSWORD = { env = 'SECRET_PASSWORD' }\nSECRET_KEY = { env = 'SECRET_KEY', default = 'this-is-a-secret' }\n```\n\n### Arrays\n\nAdd items to an array by using the `insert` key.\n\n```toml\n[tool.django]\nALLOWED_HOSTS = { insert = '127.0.0.1' }\n```\n\n## Example Integrations \ud83d\udc9a\n\n### Django\n\nThis will override any variables defined in `settings.py` with settings from the TOML files.\n\n```python\n# settings.py\nfrom pathlib import Path\nfrom dj_toml_settings import configure_toml_settings\n\nBASE_DIR = Path(__file__).resolve().parent.parent\n...\n\nconfigure_toml_settings(base_dir=BASE_DIR, data=globals())\n```\n\n### [nanodjango](https://nanodjango.readthedocs.io) \n\n```python\n# app.py\nfrom pathlib import Path\nfrom dj_toml_settings import get_toml_settings\n\nbase_dir = Path(__file__).resolve().parent\napp = Django(**get_toml_settings(base_dir=base_dir))\n\n...\n```\n\n### [coltrane](https://coltrane.adamghill.com)\n\n```python\n# app.py\nfrom pathlib import Path\nfrom django.core.management import execute_from_command_line\nfrom dj_toml_settings import get_toml_settings\nfrom coltrane import initialize\n\nbase_dir = Path(__file__).resolve().parent.parent\nwsgi = initialize(**get_toml_settings(base_dir=base_dir))\n\nif __name__ == \"__main__\":\n    execute_from_command_line()\n\n...\n```\n\n## Precedence \ud83d\udd3b\n\nThis is the order that files and sections are parsed (by default). The later sections override the previous settings.\n\n1. `pyproject.toml` -> `[tool.django]`\n2. `pyproject.toml` -> `[tool.django.apps.*]`\n3. `pyproject.toml` -> `[tool.django.envs.*]` that match `ENVIRONMENT` environment variable\n4. `django.toml` -> `[tool.django]`\n5. `django.toml` -> `[tool.django.apps.*]`\n6. `django.toml` -> `[tool.django.envs.*]` that match `ENVIRONMENT` environment variable\n\n## Specify a TOML file \ud83e\udd13\n\n```python\nfrom pathlib import Path\nfrom dj_toml_settings import get_toml_settings\n\nbase_dir = Path(__file__).resolve().parent\ntoml_settings = get_toml_settings(base_dir=base_dir, toml_settings_files=[\"custom-settings.toml\"])\n...\n```\n\n## Test \ud83e\uddea\n\n- `uv install pip install -e .[dev]`\n- `just test`\n\n## Inspiration \ud83d\ude0d\n\n- [django-pyproject](https://github.com/Ceterai/django-pyproject)\n- [django-settings-toml](https://github.com/maxking/django-settings-toml)\n",
    "bugtrack_url": null,
    "license": "The MIT License (MIT)\n         \n         Copyright (c) 2020 Ceterai\n         Copyright (c) 2025 adamghill\n         \n         Permission is hereby granted, free of charge, to any person obtaining a copy of\n         this software and associated documentation files (the \"Software\"), to deal in\n         the Software without restriction, including without limitation the rights to\n         use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n         the Software, and to permit persons to whom the Software is furnished to do so,\n         subject to the following conditions:\n         \n         The above copyright notice and this permission notice shall be included in all\n         copies or substantial portions of the Software.\n         \n         THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n         IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\n         FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\n         COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\n         IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n         CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Load Django settings from a TOML file",
    "version": "0.2.0",
    "project_urls": {
        "homepage": "https://github.com/adamghill/dj-toml-settings/",
        "repository": "https://github.com/adamghill/dj-toml-settings.git"
    },
    "split_keywords": [
        "django",
        " web",
        " toml"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ad662098c737c9a9a66c4ef22ab2dad71884f8151e4ad05602f84d61c7035b09",
                "md5": "a028c75dca0abf28682f3eac8b604d52",
                "sha256": "e2d3cebd82a583cc87e93b0f92b1d705a0b40f28247c7a26fcd1d0aa64cccebc"
            },
            "downloads": -1,
            "filename": "dj_toml_settings-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a028c75dca0abf28682f3eac8b604d52",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 6632,
            "upload_time": "2025-08-28T02:17:37",
            "upload_time_iso_8601": "2025-08-28T02:17:37.213798Z",
            "url": "https://files.pythonhosted.org/packages/ad/66/2098c737c9a9a66c4ef22ab2dad71884f8151e4ad05602f84d61c7035b09/dj_toml_settings-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ac8c1b3abbf718b2358d13df00e204ec5272a27f983ed48e9b9c3d774e10ef07",
                "md5": "c3b02b713e9f16fa78a09117d3de6167",
                "sha256": "b1890399274427c48d1806e82b9b919903b3d15393cd6dc0ef1145d1c2568a1e"
            },
            "downloads": -1,
            "filename": "dj_toml_settings-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c3b02b713e9f16fa78a09117d3de6167",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 6047,
            "upload_time": "2025-08-28T02:17:38",
            "upload_time_iso_8601": "2025-08-28T02:17:38.641284Z",
            "url": "https://files.pythonhosted.org/packages/ac/8c/1b3abbf718b2358d13df00e204ec5272a27f983ed48e9b9c3d774e10ef07/dj_toml_settings-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-28 02:17:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "adamghill",
    "github_project": "dj-toml-settings",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "dj-toml-settings"
}
        
Elapsed time: 0.46097s