All articles
2 min read

Essential Django Commands Cheat Sheet

A practical reference for the Django manage.py commands you use every day: runserver, migrations, shell, testing, static files, and more.

manage.py is Django's command-line utility for administrative tasks. This cheat sheet covers the commands you'll use regularly, with a brief note on what each one does.

Project Setup

Create a new project:

django-admin startproject myproject
cd myproject

Create a new app:

python manage.py startapp myapp

Running the Dev Server

python manage.py runserver             # default: http://127.0.0.1:8000
python manage.py runserver 0.0.0.0:8080  # accessible on your network

Database Migrations

# Detect model changes and create migration files
python manage.py makemigrations

# Apply pending migrations to the database
python manage.py migrate

# Show migration status for all apps
python manage.py showmigrations

# Show the SQL that a migration will run (without executing it)
python manage.py sqlmigrate myapp 0001

# Unapply all migrations for an app (reverting to zero)
python manage.py migrate myapp zero

Reset migrations during development (when you want a clean slate):

# 1. Delete migration files (keep __init__.py)
# 2. Drop and recreate the database (or drop tables)
python manage.py migrate --run-syncdb

User Management

# Create an admin superuser
python manage.py createsuperuser

# Change a user's password
python manage.py changepassword username

Interactive Shell

# Open a Python shell with Django context loaded
python manage.py shell

# Open the database shell (psql, sqlite3, etc.)
python manage.py dbshell

In the shell, you can import models and run queries:

from myapp.models import Post
Post.objects.filter(published=True).count()

Testing

# Run all tests
python manage.py test

# Run tests for a specific app
python manage.py test myapp

# Run a specific test class or method
python manage.py test myapp.tests.PostTests.test_create

# Run tests with verbosity
python manage.py test --verbosity=2

Static Files

# Collect all static files into STATIC_ROOT (for production)
python manage.py collectstatic

# Find where a specific static file is located
python manage.py findstatic css/main.css

Data Management

# Export data to JSON
python manage.py dumpdata myapp > myapp_data.json
python manage.py dumpdata myapp.Post --indent=2 > posts.json

# Import data from a fixture
python manage.py loaddata myapp_data.json

# Remove stale content type entries
python manage.py remove_stale_contenttypes

# Clear all data from tables (preserves schema)
python manage.py flush

Inspecting the Database

# Generate models.py code from an existing database
python manage.py inspectdb

# View differences between settings and the current database
python manage.py diffsettings

System Check

# Run Django's system check framework (finds common errors)
python manage.py check

# Check for deployment-readiness issues
python manage.py check --deploy

check --deploy is especially useful before going live — it flags missing SECRET_KEY, DEBUG=True, insecure cookie settings, and more.

Custom Management Commands

Place custom commands in myapp/management/commands/mycommand.py:

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = "My custom command description"

    def handle(self, *args, **options):
        self.stdout.write("Running custom command...")

Run it with:

python manage.py mycommand

Virtual Environment Quickstart

python -m venv venv

# Activate
source venv/bin/activate     # macOS/Linux
venv\Scripts\activate.bat    # Windows

# Install dependencies
pip install -r requirements.txt

# Deactivate
deactivate

Conclusion

python manage.py help lists every available command, and python manage.py <command> --help shows options for a specific command. The official Django documentation is the authoritative reference for all management commands and their flags.