Django Commands Cheat Sheet

Sidebar

Programming · Django Commands Cheat Sheet

The commands for an everyday Django project, starting with a copy-and-paste sequence that creates my_project from nothing. Every command in the sequence is preceded by pwd so you can see which directory you are in before it runs. Written against Django 6.1.

Start Here: A New Project From Nothing (Linux)

# Run these in order, one at a time. After each "pwd", compare the path with the "expect" note.

# 1. Make the project folder (works from any directory)
pwd
mkdir -p /home/philip/Projects/my_project

# 2. Go into it. Every later step runs from here unless it says otherwise.
pwd
cd /home/philip/Projects/my_project

# 3. Create the virtual environment, a folder named .venv inside the project
pwd                                   # expect: /home/philip/Projects/my_project
python3 -m venv .venv

# 4. Activate it. Your prompt should now start with (.venv)
pwd                                   # expect: /home/philip/Projects/my_project
source .venv/bin/activate

# 5. Upgrade pip inside the venv (a new venv often ships an old one)
pwd                                   # expect: /home/philip/Projects/my_project
python -m pip install --upgrade pip

# 6. Install Django into the venv
pwd                                   # expect: /home/philip/Projects/my_project
pip install django

# 7. Create the project. The dot at the end means "here", so manage.py lands in this folder
pwd                                   # expect: /home/philip/Projects/my_project
django-admin startproject my_project .

# 8. Create an app called blog. Run it where manage.py is
pwd                                   # expect: /home/philip/Projects/my_project
ls                                    # expect to see manage.py in the list
python manage.py startapp blog

# 9. Register the app: edit my_project/settings.py and add 'blog' to INSTALLED_APPS (not a command)

# 10. Create the database tables for Django's built-in apps
pwd                                   # expect: /home/philip/Projects/my_project
python manage.py migrate

# 11. Create an admin login (it asks for a username, email and password)
pwd                                   # expect: /home/philip/Projects/my_project
python manage.py createsuperuser

# 12. Save the package list so the environment can be rebuilt
pwd                                   # expect: /home/philip/Projects/my_project
pip freeze > requirements.txt

# 13. Start the development server, then open http://127.0.0.1:8000/ (Ctrl+C stops it)
pwd                                   # expect: /home/philip/Projects/my_project
python manage.py runserver

What You End Up With

my_project/              # the folder you made (the "project root")
├── .venv/               # the virtual environment
├── manage.py            # the command runner
├── requirements.txt
├── db.sqlite3           # appears after migrate
├── my_project/          # the project's configuration package
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── asgi.py
│   └── wsgi.py
└── blog/                # the app
    ├── migrations/
    ├── admin.py
    ├── apps.py
    ├── models.py
    ├── tests.py
    └── views.py

Coming Back Another Day

cd /home/philip/Projects/my_projectGo to the project root first
source .venv/bin/activateLook for (.venv) at the start of the prompt
python manage.py runserverStart the server
deactivateLeave the venv when you are finished
pip install -r requirements.txtOn a fresh clone: after creating and activating a new venv

The Development Server

python manage.py runserverServes on 127.0.0.1:8000 and restarts when you save a file
python manage.py runserver 8001Use a different port
python manage.py runserver 0.0.0.0:8000Reachable from other devices; add the host to ALLOWED_HOSTS if you get a 400
Ctrl+CStop the server
python manage.py checkLook for configuration problems without starting anything
python manage.py check --deploySecurity checklist for production (a new project reports 8 warnings)

Apps

python manage.py startapp shopCreates the folder shop/ with models, views, admin, tests and migrations
'blog'Add this string to INSTALLED_APPS in settings.py after creating an app
django-admin startproject name .Project: only once, and with the dot to avoid an extra nested folder
python manage.py startapp --helpOptions, including --template

Models and Migrations

python manage.py makemigrationsAfter changing a model: writes the migration files
python manage.py makemigrations blog -n add_bodyOnly for one app, with your own name for the file
python manage.py migrateApplies every unapplied migration to the database
python manage.py showmigrations[X] applied, [ ] not yet
python manage.py sqlmigrate blog 0001Prints the SQL a migration would run
python manage.py migrate blog zeroUndoes all of one app's migrations
python manage.py makemigrations --check --dry-runChanges nothing; exit code 1 if a migration is missing (useful in CI)

Users and the Admin

python manage.py createsuperuserPrompts for username, email and password
python manage.py changepassword adminReset a user's password
python manage.py createsuperuser --noinput --username admin --email a@b.comFor scripts, with no prompts: set DJANGO_SUPERUSER_PASSWORD in the environment first (worked here)
/admin/The admin site's URL: http://127.0.0.1:8000/admin/
python manage.py clearsessionsDeletes expired sessions

The Shell and Data

python manage.py shellPython prompt with Django loaded; it imports your models for you
python manage.py shell -c "..."Run one line of Python and exit
python manage.py dumpdata blog --indent 2 > blog.jsonExport an app's rows as JSON
python manage.py loaddata blog.jsonImport them again
python manage.py inspectdbPrint model code for an existing database's tables
python manage.py flushEmpties every table (it asks first). Keeps the tables
python manage.py dbshellOpens the database's own client (needs sqlite3 installed; not run here)

Tests

python manage.py testRuns every test in the project
python manage.py test blogOnly one app
python manage.py test blog.tests.PostTests.test_createOne test method
python manage.py test -v 2Verbose: shows each test and the database setup
python manage.py test --keepdbReuse the test database between runs
python manage.py test --parallelSpread tests across processes
"NO TESTS RAN"A new app has an empty tests.py; test names must start with test_

Static Files

STATIC_ROOT = BASE_DIR / 'staticfiles'Add to settings.py first, or the next command fails
python manage.py collectstaticCopies every app's static files into STATIC_ROOT (130 for a new project)
python manage.py collectstatic --noinputWithout the yes/no prompt
python manage.py findstatic admin/css/base.cssShows which file Django would serve for a path

Settings Worth Knowing

python manage.py diffsettingsLists the settings that differ from Django's defaults
DEBUGTrue in development only
SECRET_KEYGenerated for you and marked django-insecure-; replace and keep out of git for a real site
ALLOWED_HOSTSMust list your domain once DEBUG is off
DATABASESSQLite file db.sqlite3 by default
python manage.py helpLists every command available; help <command> for one

Packages and the venv

pip install djangorestframeworkInstall into the active venv only
pip freeze > requirements.txtRun again whenever you add a package
python -m django --versionCheck the installed Django version
pip list --outdatedSee which packages have newer versions
.gitignoreAdd .venv/, db.sqlite3, staticfiles/ and __pycache__/

Troubleshooting

django-admin: command not foundThe venv is not active. Check the prompt for (.venv) and run the activate line
can't open file 'manage.py'Wrong directory. Run pwd and ls; you need the folder that contains manage.py
No module named 'django'Not installed in this venv, or a different Python is running. Check which python
You have N unapplied migration(s)Run python manage.py migrate
That port is already in use.Another server is running (or use a different port: runserver 8001). The wording comes from Django's source; I did not trigger it here
Model or template of a new app is ignoredThe app is not in INSTALLED_APPS
ensurepip is not availableDebian/Ubuntu: install the python3-venv package, then repeat step 3 (from general knowledge, not run here)
The three questions behind most beginner errors
Which directory am I in (pwd, and does ls show manage.py)? Is the venv active (the (.venv) prefix)? Did I add the app to INSTALLED_APPS? Almost every "it does nothing" or "command not found" message in this sheet is one of those three.
What was checked on this sheet
I ran the commands from "Start here" onward in a scratch project with Python 3.14.0 and Django 6.1.2 on Windows 11 (startproject, startapp, makemigrations, migrate, createsuperuser, shell, dumpdata, loaddata, test, collectstatic, findstatic, diffsettings, check, clearsessions, flush (declined at its prompt), runserver with real requests, including a 400 for an unknown host on 0.0.0.0). Not run: the Linux-specific steps (mkdir -p, python3 -m venv, source .venv/bin/activate and the /home/philip/... paths, which differ only in syntax from Windows), dbshell (no sqlite3 program here), and the port-in-use error.