.env · security🔒 100% client-side — never leaves your browser

Django Secret Key Generator

Generate a Django secret key online: this Django secret key generator creates a 50-character SECRET_KEY from the same character set as Django's own get_random_secret_key(), using your browser's cryptographic random generator.

By Aliniyaz Mamat · last reviewed

Be the first to rate
Django SECRET_KEY (50 chars)
Matches Django's get_random_secret_key() charset
Raw secret
.env line
SECRET_KEY=''
CLI alternative (Python)
from django.core.management.utils import get_random_secret_key print(get_random_secret_key())

Key takeaways

  • ✓Same format as Django: 50 characters from a-z, 0-9 and !@#$%^&*(-_=+), about 282 bits of randomness.
  • ✓Passes Django's deployment check (security.W009): at least 50 characters, at least 5 unique ones, no django-insecure- prefix.
  • ✓The .env line is single-quoted because the key can contain $ and #, which Docker Compose and some loaders would otherwise interpret.
  • ✓Changing SECRET_KEY logs out every user and invalidates password-reset links. Since Django 4.1, SECRET_KEY_FALLBACKS lets you rotate without that.

What it does

  • ▸50-character key in Django's exact get_random_secret_key() format
  • ▸Unbiased selection from crypto.getRandomValues (like Python's secrets.choice)
  • ▸Ready-to-paste .env line with safe quoting
  • ▸One-click regenerate
  • ▸Generated locally, nothing is sent or stored

Privacy

Runs 100% in your browser. Your .env never touches our servers.

client-side only

When to use this tool

  • →A fresh startproject whose settings.py contains a django-insecure-… key
  • →Moving the key from settings.py into an environment variable before the first deploy
  • →A separate key per environment: development, staging and production must not share one
  • →Rotating the key after it appeared in a git commit, a log or a screenshot

Common mistakes

  • ⚠Keeping the django-insecure- key from startproject in production. manage.py check --deploy flags it
  • ⚠Committing the key in settings.py. Once in git history, treat it as leaked and rotate
  • ⚠Using the same key for staging and production: a staging leak then compromises production sessions
  • ⚠Rotating without SECRET_KEY_FALLBACKS, which logs out every user at once

What Django signs with SECRET_KEY

SECRET_KEY is the HMAC key behind Django's signing framework. Anyone who knows it can create values Django will trust:

FeatureWhat an attacker with the key can do
Sessions (signed cookie backend)forge a session cookie and log in as any user
Password reset tokenscreate valid reset links for any account
django.core.signing / TimestampSignerforge any signed value your code trusts
Messages framework (cookie storage)inject messages
CSRF tokensnot directly (CSRF uses its own secret), but keep the key secret anyway

Database-backed sessions are not forgeable with the key alone, but password reset tokens are. In practice: a leaked SECRET_KEY is an account-takeover risk and must be rotated.

Moving SECRET_KEY out of settings.py

  1. Generate a key above and add the single-quoted line to .env (and .env to .gitignore).
  2. Read it in settings.py. With django-environ:
settings.py
import environ
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
env = environ.Env()
environ.Env.read_env(BASE_DIR / ".env")

SECRET_KEY = env("SECRET_KEY")  # ImproperlyConfigured if missing
SECRET_KEY_FALLBACKS = env.list("SECRET_KEY_FALLBACKS", default=[])
  1. In production, set SECRET_KEY as a real environment variable on the host (Heroku config vars, Render, Fly secrets, a Kubernetes Secret) instead of shipping a .env file.
  2. Run python manage.py check --deploy to confirm Django accepts the key.

The Django .env generator builds a complete .env for Django with DEBUG, ALLOWED_HOSTS, Postgres, Redis and Celery keys around this one.

Rotating the key without logging everyone out

Django 4.1 added SECRET_KEY_FALLBACKS: new values are signed with SECRET_KEY, and values signed with any key in the fallback list are still accepted.

# 1. new key becomes SECRET_KEY, old key moves to the fallbacks
SECRET_KEY='<new key>'
SECRET_KEY_FALLBACKS='<old key>'

# 2. after SESSION_COOKIE_AGE has passed (default 2 weeks), remove the fallback
SECRET_KEY_FALLBACKS=

If the old key leaked, skip the fallback: every token signed with it must stop working now. On Django versions before 4.1 there is no fallback setting, so rotation always ends all sessions.

Other ways to generate a Django secret key

# Django itself (needs Django installed)
python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"

# Python standard library only
python -c "import secrets; print(secrets.token_urlsafe(50))"

# OpenSSL
openssl rand -base64 48

All three are fine: Django only requires length and randomness, not a specific alphabet. For other frameworks, see the Rails secret key generator, the Laravel APP_KEY generator or the general secret generator.

Frequently asked questions

How long should a Django SECRET_KEY be?

At least 50 characters with at least 5 different characters, otherwise manage.py check --deploy reports security.W009. This generator produces exactly 50 characters, the same length as Django's get_random_secret_key().

Is a key with uppercase letters or other symbols allowed?

Yes. Django accepts any string; the lowercase alphabet is just what get_random_secret_key() uses. secrets.token_urlsafe(50) output works just as well.

What happens if I change SECRET_KEY?

Signed sessions, password-reset links and other signed values become invalid, so users are logged out. Add the old key to SECRET_KEY_FALLBACKS (Django 4.1+) to avoid that during a planned rotation.

Why is the .env value in single quotes?

The key can contain $ and #. Unquoted, Docker Compose may try to expand $abc as a variable and some parsers cut the value at #. Single quotes keep the value literal in python-dotenv, django-environ and Compose.

Is the generated key stored anywhere?

No. It is generated with crypto.getRandomValues in your browser and never sent to a server. Reloading the page creates a new one.

Related tools

Learn more

Compare

coming soon

Get notified when env syncing launches

We're building a tiny tool to keep .env files in sync across teammates and environments. Leave your email — no spam, just a single launch ping.

We use your address only for this launch email. You can ask us to delete it at any time. Privacy policy