DEV Community

Cover image for Generating SECRET_KEY for production deployment of Django project
Shakhzhakhan Maxudbek
Shakhzhakhan Maxudbek

Posted on • Originally published at args.tech

Generating SECRET_KEY for production deployment of Django project

Django SECRET_KEY need for providing cryptographic signing (documentation). This value is stored in <your_app>/settings.py file. When you start new project it will be generated from built-in function (source code). For production deployment SECRET_KEY must be strong and reliably protected.

These steps help you for generating new SECRET_KEY value:

Activate your project's virtual environment:

source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Enter in Django's manage.py shell:

python3 manage.py shell
Enter fullscreen mode Exit fullscreen mode

Import get_random_secret_key() function:

from django.core.management.utils import get_random_secret_key
Enter fullscreen mode Exit fullscreen mode

Just call get_random_secret_key() function:

get_random_secret_key()
Enter fullscreen mode Exit fullscreen mode

Full example:

user@localhost:~/demo$ source env/bin/activate
(env) user@localhost:~/demo$ python3 manage.py shell
Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.management.utils import get_random_secret_key
>>> get_random_secret_key()
'(#jdao11q1)kw1rs40z2$b^kntmw3ts9)wg2r*zk3z0_^t&hha'
Enter fullscreen mode Exit fullscreen mode

Are my posts is helpful? You may support me on Patreon.

Top comments (0)