$ python manage.py shell -c 'from django.core.management import utils; print(utils.get_random_secret_key())'
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# generate_secret.py
from django.core.management import utils
print(utils.get_random_secret_key())
### you can use this approach to secure any variable not only the secret key ###
### 1- in settings.py ###
from decouple import config
SECRET_KEY = config('SECRET_KEY') # replace your line with this line
### 2- add SECRET_KEY into Environmental Variables ###
### 1- in Development ###
### create a .env file with the following line ###
SECRET_KEY = myKey # replace my key with your key (without quotes)
### 2- in Production ###
### just add it to your Production System's Environmental Variables ###
$ python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'