Generate new secret keys in Django
Every Django project needs a secret key.
When we create a project using
django-admin startproject mysite command,
Django by default gives us a secret key in the
Now for development purposes the provided key is good, but when we have to deploy the project in production, we need to use a new unique key. There are various methods to generate a key, here I list some of them.
Using method provided by Django
Django generates new secret key every time we create a project, we can also use the same functionality that django provides to generate a key for us.
For Django 1.10 and above, following code snippet can be used.
from django.core.management.utils import get_random_secret_key get_random_secret_key()
For older version:
from django.utils.crypto import get_random_string chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' get_random_string(50, chars)