Skip to main content

A package for storing Django settings in pyproject.toml.

Project description

django-pyproject

Description

This package allows you to store some/all of your settings in your pyproject.toml file (or any toml file in general).

Installation

You can add django-pyproject to your poetry project with:

poetry add django-pyproject

Or through pip:

pip install django-pyproject

Usage

You can use django-pyproject to import any settings specified in your pyproject file under [tool.django].

Settings file

To import django settings from your pyproject file, use this in your settings file:

from djpp import pyproject
pyproject.load()

This will work only if you have a standard django project structure.
If your pyproject file is located somewhere else or has a different name, you can specify it:

pyproject.load('path-to-your-pyproject-file')

PyProject file

All django settings in pyproject.toml file should be stored under [tool.django] key, like this:

[tool.django]
ALLOWED_HOSTS = []

You don't have to use uppercase letters for the variable names, django-pyproject will automatically convert them all. This does not work for dict key names:

[tool.django]
allowed_hosts = []

[tool.django.databases.default]
engine = 'django.db.backends.sqlite3'
HOST = '127.0.0.1'
PORT = '5432'

Will convert into:

ALLOWED_HOSTS = []
DATABASES = {
    'default': {
        'engine': 'django.db.backends.sqlite3',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

But what to do about relative filepaths, that you had to construct with os.path?
You can specify filepaths separating them with '/' in inline dict under key 'path'.
Using '..' will make django-pyproject go up a level. Starting with '.' will make a path relative:

base_dir = { path = "." }
project_dir = { path = "./your_project_folder" }
repo_dir = { path = "./.." }

This will have the same effect as the following code in settings.py:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
REPO_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

This is assuming you have a standard django project structure.

If a value needs to be taken from an environment, use inline dict with key 'path' and optional key 'default':

email_host_password = { env = 'SECRET_PASSWORD' }
secret_key = { env = 'SECRET_KEY', default = 'hello' }

Docker & Production

If some of you settings have an alternative value for when DEBUG is off, specify them in [tool.django.production]. They will override regular settings if DEBUG is off.

By default, django-pyproject applies production settings and sets DEBUG to False, if current evironment has a DJANGO_ENV variable, set to 'production'.
You can override it with your own key and value like this:

pyproject.load(production_env=('YOUR_KEY', 'your_value'))

If some of you settings have an alternative value for when the app is in container, specify them in [tool.django.docker]. They will override regular settings and will be overriden by production settings.

By default, django-pyproject applies docker settings, if current evironment has a DJANGO_ENV variable.
You can override it with your own key like this:

pyproject.load(docker_env='YOUR_KEY')

Concatenating & Poetry keys

You can concat strings using 'concat' key with a list of strings.
Additionally, you can access values in keys from [tool.poetry] using 'poetry' key.
This example explains both features:

[tool.poetry]
name = "my-app"
version = "0.0.1"

[tool.django]
project_name = { con = { poetry = "name" }, cat = ", v.1" }

will result in

PROJECT_NAME = 'my-app, v.1'

Keep in mind, that due to toml python library limitations, enclosing too much inline dicts in each other may result in error.
To overcome this, use poetry-cat hybrid:

[tool.poetry]
name = "my-app"
version = "0.0.1"

[tool.django]
project_name = { poetry = "name", cat = ", v.1" }

will result in

PROJECT_NAME = 'my-app, v.1'

Apps

You can group settings that belong to an external app together for easier access.
To do that, you can list them under [tool.django.apps.your_app].
You can also modify variables like INSTALLED_APPS from here with 'insert' key.

Here's an example for corsheaders:

[tool.django.apps.cors]
CORS_ORIGIN_WHITELIST = ['http://localhost:3000',]
CORS_ALLOW_CREDENTIALS = true
CSRF_COOKIE_NAME = "XCSRF-Token"
INSTALLED_APPS = { insert = 'corsheaders' }
MIDDLEWARE = { insert = 'corsheaders.middleware.CorsMiddleware', pos = 3 }

This is similar to the following python code:

CORS_ORIGIN_WHITELIST = ('http://localhost:3000',)
CORS_ALLOW_CREDENTIALS = True
CSRF_COOKIE_NAME = "XCSRF-Token"
INSTALLED_APPS.append('corsheaders')
MIDDLEWARE.insert(3, 'corsheaders.middleware.CorsMiddleware')

Full import

You also can simply import pyproject file (or any toml file) contents as a dict with load_all().

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

django-pyproject-1.0.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

django_pyproject-1.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file django-pyproject-1.0.2.tar.gz.

File metadata

  • Download URL: django-pyproject-1.0.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.1 Windows/10

File hashes

Hashes for django-pyproject-1.0.2.tar.gz
Algorithm Hash digest
SHA256 1c6cdd6f7c17d31d9ef009e169a64d099de025e60693c1e5d72543f6cdf735df
MD5 bec3df90e7630ea64a104d42a5a57ed3
BLAKE2b-256 d0c53bf14e57384e1076842d5f81100b3132bea166a7df046621f9303723f280

See more details on using hashes here.

File details

Details for the file django_pyproject-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_pyproject-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.1 Windows/10

File hashes

Hashes for django_pyproject-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1d718d64cee7623c8b7735cd94982ba6e162e53dc3ec3ad5ca942730cfb7bd9e
MD5 ba798107ee391425bab524c4c85a82be
BLAKE2b-256 f330636fc8730f65e9d9f5b6047291f1af5ca34ffcb898368a4fd948cb0ce25d

See more details on using hashes here.

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