Skip to content

Instantly share code, notes, and snippets.

@isoboroff
Created February 18, 2021 20:07
Show Gist options
  • Save isoboroff/48986e7df9ae067fcfd6c62d3c911385 to your computer and use it in GitHub Desktop.
Save isoboroff/48986e7df9ae067fcfd6c62d3c911385 to your computer and use it in GitHub Desktop.
Django ./manage.py command to generate secret keys
from django.core.management.base import BaseCommand, CommandError
from django.utils.crypto import get_random_string
class Command(BaseCommand):
help='''Generate a secret key the same way Django does.
You will need to install it either in settings.py for dev or externally for production
'''
def add_arguments(self, parser):
parser.add_argument('-l', '--length', type=int, default=50)
def handle(self, *args, **options):
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return get_random_string(options['length'], chars)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment