Skip to main content

The easy way of handling Django secrets.

Project description

This little app helps you to not commit your secrets to a repo and adds a nice way of exporting/importing secrets for a new deployment or automated testing with environment variables.

Though this never happened to me during coding, Travis had some problems to reliably execute the tests and hot load the new secrets, so I had move the secrets module into the my_secrets package to not have two modules named secrets. Python obviously cant handle that. Lesson learned after about 40 Travis builds. :(

django-secrets is currently tested with Python 3.12, 3.13, and 3.14, and with Django 4.x, 5.x, and 6.x.

Supported versions

The CI matrix currently covers:

  • Python 3.12, 3.13, and 3.14

  • Django 4.x, 5.x, and 6.x

  • Django 4.x only on Python 3.12

For Django’s official Python compatibility, see the release notes: Django 5.2 and Django 6.1.

Installation

pip install django-secrets

If you prefer uv, use:

uv add django-secrets

Usage

After installing the package please add it to your INSTALLED_APPS settings.

INSTALLED_APPS = (
    ...
    'django_secrets',
)

Because we want to be able to hide our secret settings, we have to alter manage.py to run some code before the Django magic happens. Open manage.py and alter it like this:

if __name__ == "__main__":

    from django_secrets.startup import check
    check()

    ...

Now that the check is in place, run manage.py to initialize your project. This will create a new my_secrets package in your project root with the following contents:

my_secrets
├── .gitignore
├── __init__.py
├── definitions.py
└── secrets.py

The package also features a .gitignore file to prevent you from checking in any secrets to git. Now open definitions.py to add your secrets to the list. Start with the Django secret key for example. When you are done adding all secrets, run manage.py again and you will be asked to enter your secrets.

Now you can remove your secrets from settings.py and instead replace them like this:

from my_secrets import secrets

SECRET_KEY = secrets.SECRET_KEY

Since the secrets are saved in a normal python package, you can just use them the normal way including code completion, but now they are safe! :)

Import / Export

This package adds a new management command: export_secrets. This will print out export statements so you can easily create environment variables on a new machine and let the check function do the rest for you, because it will also read in any known environment variables as secret values. This is quite handy for dynamically spawned instances or CI testing.

Have fun and stay safe!

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_secrets-3.0.3.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

django_secrets-3.0.3-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file django_secrets-3.0.3.tar.gz.

File metadata

  • Download URL: django_secrets-3.0.3.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for django_secrets-3.0.3.tar.gz
Algorithm Hash digest
SHA256 d292c18eb87cceb0076fabb660db5b7dd004d5cbe1acc16ff9468e3c2bef80dd
MD5 3ea6d2c0befc3775547d50612d307c56
BLAKE2b-256 bd2b0ee3bc1d7326a9b0b82c0a3d21c4c936f1565e0d4f88b65db1869036787e

See more details on using hashes here.

File details

Details for the file django_secrets-3.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_secrets-3.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c38136cd4da9768527d94ec16843b0e1f06cf3da003dacc2027f1aea4864770e
MD5 26ee843e6fc6ca50ea476dff9120e51b
BLAKE2b-256 fa17becac6157447648523819545c8e7773952bcb9db98b901ac5d11d1add2b5

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