A simple Django app to store secret keys outside of settings.py
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
In your project's settings.py, import the app like so:
from autosecretkey import AutoSecretKey
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.iniin your project's base directory (i.e. the one containing
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.
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:
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
This is a simple example you could have in your
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.
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.
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.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for django-autosecretkey-0.9.2.tar.gz
Hashes for django_autosecretkey-0.9.2-py3-none-any.whl