Skip to main content

django-autosecretkey

autosecretkey is a simple reusable Django app which will make it easier to distribute your Django project by taking care of generating a secure SECRET_KEY and storing it outside of your project's settings.py

Quick start

  1. In your project's settings.py, import the app like so:

    from autosecretkey import AutoSecretKey
    
  2. Still in the settings.py file, replace the existing SECRET_KEY line with something like this:

    SECRET_KEY = AutoSecretKey(BASE_DIR / "config.ini").secret_key
    

    (N.B.: Of course, this line has to be after the BASE_DIR line.)

    This will store the key in a file called config.ini in your project's base directory (i.e. the one containing manage.py).

    Make sure not to ship this file with your code distribution. For example, you may want to add it to your .gitignore file if you publish the project in a git repository.

Additional configuration

For additional security, you may want to store your secret key in a different location than your project's base directory. You could, for example, do something like this:

AutoSecretKey("/etc/your_project/config.ini")

You need to manually make sure that the user your Django project runs as has the permission to read and write this file. Running something like this as root should do the trick in Linux (replacing "djangouser" with the actual user name):

mkdir /etc/your_project/
touch /etc/your_project/configuration
chown djangouser /etc/your_project/configuration

In the end, this is just a simple wrapper around configparser.ConfigParser, so you can store custom configuration values in the file that holds your secret key. You can access the ConfigParser object as the config attribute of your AutoSecretKey object.

This is a simple example you could have in your settings.py:

from autosecretkey import AutoSecretKey
my_config_file = AutoSecretKey(BASE_DIR / "config.ini")
SECRET_KEY = my_config_file.secret_key
TIME_ZONE = my_config_file.config["MY_SETTINGS"]["TIME_ZONE"]

For reference, the corresponding config.ini might look like this:

[AutoSecretKey]
SecretKey = WellThisIsWhereYouWillFindYourSecretKey

[MY_SETTINGS]
TIME_ZONE = UTC

You can pass the path to an .ini file to use as a template when first creating the file creating your secret key. This file may contain any additional settings you want to have in your config file, the SecretKey will then be added to that. Note that you must not define a secret key within that template file.

AutoSecretKey("/etc/myproject/config.ini", template=BASE_DIR/"config.dist.ini")

You can also set the create attribute to False if you need to make sure the config file already exists - you may want to use this to make sure that custom settings have already been made. If the file exists but no secret key is defined within it, a new secret key will be added to the file.

AutoSecretKey("config.ini", create=False)

All methods you can use on any other ConfigParser object can be used on that object as well, of course, like get(), getboolean(), etc. For convenience, you can use the AutoSecretKey object's update() method to re-read the contents of the config file, and the write() method to write back any changes you have made on the object to the configuration file.

Note that the ConfigParser behaves like a RawConfigParser in that it does not support interpolation.

Metadata

Release files for django-autosecretkey 0.9.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-autosecretkey 0.9.2
File Size Uploaded
django-autosecretkey-0.9.2.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-autosecretkey 0.9.2
File Interpreter ABI Platform
django_autosecretkey-0.9.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / django-autosecretkey-0.9.2.tar.gz

Download URL django-autosecretkey-0.9.2.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
29e8411afe47ffcab435df998117c34d9d49b24a9b0ac3ba13baa820cf1b0020
BLAKE2b-256 checksum
How to use checksums
6408bcdd7b9710eb00394efc89b7db62271af1227a0dc32e462238e2a2d21119
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / django_autosecretkey-0.9.2-py3-none-any.whl

Download URL django_autosecretkey-0.9.2-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48b5048acd24795be37416a229579a4377f525dfc292fcb40e39deef60a18183
BLAKE2b-256 checksum
How to use checksums
3d52b5c7b62bfa678b4e28035ae20bd5e8a0292fdd9cfb9398e12fe5f2774d7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.9.2 This release

2 release files

0.9.1

2 release files

0.9

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page