Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Django secret key generator
"""
Pseudo-random django secret key generator.
- Does print SECRET key to terminal which can be seen as unsafe.
"""
import string
import random
from __future__ import print_function
# Get ascii Characters numbers and punctuation (minus quote characters as they could terminate string).
chars = ''.join([string.ascii_letters, string.digits, string.punctuation]).replace('\'', '').replace('"', '').replace('\\', '')
SECRET_KEY = ''.join([random.SystemRandom().choice(chars) for i in range(50)])
print(SECRET_KEY)
@yozel

This comment has been minimized.

Copy link

@yozel yozel commented Oct 11, 2015

You should move from __future__ import print_function to top of the import string . Current code is trowing SyntaxError: from __future__ imports must occur at the beginning of the file exception

@ariestiyansyah

This comment has been minimized.

Copy link

@ariestiyansyah ariestiyansyah commented Aug 9, 2016

I made pip installation to generate django secret key https://github.com/ariestiyansyah/django-secret-key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.