Leo's Technical Blog

Django SECRET_KEY Generation

Introduction

user

Leo Soto


python, tips, django

Django SECRET_KEY Generation

Posted by Leo Soto on .
Featured

python, tips, django

Django SECRET_KEY Generation

Posted by Leo Soto on .

New version :

The original version of this recipe is quite outdated and wasn't secure enough. Here is a better way as of March 2014:

$ python -c 'import random; import string; print "".join([random.SystemRandom().choice(string.digits + string.letters + string.punctuation) for i in range(100)])'

But you rarely need to use this unless the SECRET_KEY generated by Django (when you created the project) was compromised and needs to be changed.

Old version:

(Don't use this version, it's here just for historical purposes)

When deploying Django application it is a common step to generate a SECRET_KEY for the site. Here is the quick recipe to do it:

$ python -c 'import random; print "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for i in range(50)])'

Useful when for whatever reason you don't want to install django-command-extensions.