django / django/new-features

Add a new management command `generate_secret_key` for producing Django SECRET_KEY values

Open
#100 6 comments 24 reactions 0 assignees View on GitHub
CLI / Management Commands Django Core
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

# ✅ **Type:**

New feature

---

# ✅ **Component:**

Core (Management commands)

---

# ✅ **Description:**

## **Summary**

Django provides `django.core.management.utils.get_random_secret_key()` for generating cryptographically secure SECRET_KEY values. However, there is currently **no management command** for generating a SECRET_KEY directly through `manage.py` or `django-admin`.

Since Django requires a strong secret key in production environments—and because developers consistently rely on external scripts, snippets, or manual copying—adding a first-class Django command improves security, usability, and consistency.

This feature proposes adding:

```
python manage.py generate_secret_key
```

Which outputs a fresh, cryptographically secure SECRET_KEY to stdout.

---

## **Motivation**

1. **Developer experience**:
Many users expect a built-in way to generate secret keys during provisioning, automation, CI, or deployment.

2. **Security**:
Encourages developers to use Django’s official key generator rather than ad-hoc scripts or insecure online generators.

3. **Consistency**:
Django already provides the key generator function internally. Exposing it as a management command follows Django’s “batteries included” philosophy.

4. **Common use case**:
Tools like Docker, Ansible, Terraform, CI/CD pipelines, and project templates often require dynamic runtime key generation.

---

## **Proposed Implementation**

Add a new command:

**File:**
`django/core/management/commands/generate_secret_key.py`

**Implementation:**

```python
from django.core.management.base import BaseCommand
from django.core.management.utils import get_random_secret_key

class Command(BaseCommand):
help = "Generate a new Django SECRET_KEY."

def handle(self, *args, **options):
self.stdout.write(get_random_secret_key())
```

Outputs a secure secret key to stdout without modifying project files.

---

## **Documentation**

Add to `docs/ref/django-admin.txt`:

```
generate_secret_key
-------------------

.. versionadded:: 5.2

Generates a cryptographically secure SECRET_KEY value using Django’s internal
key generator.

Example::

$ python manage.py generate_secret_key
h$!2sd9&l1%bn#x42v%0!hq7vr1j1d#^%gq39ap*+u

This command prints the generated key to stdout and does not modify any files.
```

---

## **Tests**

A test verifying stdout output, e.g.:

```python
from django.core.management import call_command
from django.test import SimpleTestCase
from io import StringIO

class GenerateSecretKeyTests(SimpleTestCase):
def test_key_is_generated(self):
out = StringIO()
call_command('generate_secret_key', stdout=out)
key = out.getvalue().strip()
self.assertIsInstance(key, str)
self.assertGreater(len(key), 30)
```

---

## **Conclusion**

This feature is minimal, safe, consistent with Django’s design, and provides meaningful value. Many developers already implement their own versions; Django offering it officially would standardize the workflow and prevent insecure alternatives.

### Problem

Django requires each project to define a strong and unique SECRET_KEY, especially in production environments. While Django includes a secure generator function (get_random_secret_key()), it does not provide a built-in management command for generating a secret key from the command line.

As a result:

Developers frequently create their own scripts.

Online key generators (some insecure) are used incorrectly.

Deployment pipelines, containers, and automation tools must re-implement key generation.

New users often rely on hard-coded or weak keys during setup.

This absence creates unnecessary friction and inconsistent practices around a security-critical configuration value that Django already knows how to generate securely.

### Request or proposal

proposal

### Additional Details

_No response_

### Implementation Suggestions

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.