Django Python web framework

Version 6.1 · source

Start here

django-admin startproject <name>
New project
python manage.py startapp <app>
New app
python manage.py runserver
Dev server
python manage.py makemigrations
Create migrations
python manage.py migrate
Apply migrations
python manage.py createsuperuser
Admin account
python manage.py shell
Shell with models
python manage.py test
Run tests

Concepts

  1. models.py edit models
  2. Migration file in migrations/
  3. Database schema updated
  4. Dev server localhost:8000

Project

django-admin startproject <name>
Create a project
Add a trailing . to use the current directory instead of a new folder.
python manage.py startapp <app>
Create an app in the project
Then add it to INSTALLED_APPS in settings.
python manage.py check
Run system checks
python manage.py check --deploy
Check production settings
django-admin startproject <name> --template <url>
Start from a project template
Templates can run code; inspect before use.
python manage.py diffsettings
Settings that differ from defaults
django-admin --version
Print the Django version
Generated layout
Path Purpose
manage.py project command runner
<name>/settings.py configuration
<name>/urls.py root URL routes
<app>/models.py database models
<app>/views.py request handlers
<app>/migrations/ schema history

Run

python manage.py runserver
Start the dev server
Auto-reloads on code changes.
python manage.py runserver <port>
Use another port
python manage.py runserver 0.0.0.0:8000
Listen on all interfaces
Needs the host in ALLOWED_HOSTS.
python manage.py runserver --noreload
Disable auto-reload
python manage.py shell
Python shell with Django set up
python manage.py shell -c "<code>"
Run one Python statement
python manage.py dbshell
Open the database client
python manage.py createsuperuser
Create an admin user
python manage.py changepassword <user>
Change a user's password

Migrations

python manage.py makemigrations
Create migrations from model changes
python manage.py migrate
Apply pending migrations
python manage.py makemigrations <app>
Limit to one app
python manage.py showmigrations
List migrations and applied state
python manage.py makemigrations --dry-run
Preview without writing files
python manage.py makemigrations --check
Fail if migrations are missing
Useful in CI.
python manage.py migrate <app> <migration>
Migrate to a given point
Use zero to unapply all of an app.
python manage.py sqlmigrate <app> <migration>
Print a migration's SQL
python manage.py makemigrations --empty <app>
Empty migration for custom work
python manage.py migrate --plan
Show what migrate would do
python manage.py migrate --fake <app> <migration>
Mark applied without running
Only when the schema already matches.
python manage.py squashmigrations <app> <migration>
Squash into fewer migrations

Tests

python manage.py test
Run all tests
python manage.py test <app>.tests.<Class>.<method>
Run one test
python manage.py test --keepdb
Reuse the test database
python manage.py test --parallel
Run tests in parallel
python manage.py test --failfast
Stop at first failure
python manage.py test -k <pattern>
Run tests matching a name
python manage.py test --pdb
Debugger on error or failure
python manage.py test --debug-sql
Show SQL for failing tests
python manage.py test --tag <tag>
Run tests with a tag

Data

python manage.py dumpdata <app>.<Model> --indent 2
Export data as JSON
python manage.py loaddata <fixture>
Import a fixture
python manage.py flush
Empty all tables, keep migrations
Asks for confirmation.
python manage.py inspectdb
Models from an existing database
python manage.py dumpdata --natural-foreign --exclude contenttypes
Portable export
python manage.py sqlflush
Print the SQL flush would run

Static & i18n

python manage.py collectstatic
Gather static files for deployment
Needs STATIC_ROOT set.
python manage.py collectstatic --noinput
Run without prompts
python manage.py findstatic <path>
Where a static file resolves
python manage.py makemessages -l <lang>
Extract translatable strings
python manage.py compilemessages
Compile .po to .mo
python manage.py clearsessions
Delete expired sessions
python manage.py sendtestemail <address>
Test the email setup

Shell queries

Model.objects.all()
All rows
Model.objects.filter(<field>=<value>)
Rows matching a condition
Model.objects.get(pk=<id>)
One row; raises if missing or many
Model.objects.create(<field>=<value>)
Insert a row
Model.objects.filter(<field>__icontains="<text>")
Field lookup with a suffix
print(qs.query)
Show the SQL of a queryset

Recipes

Change a model
# edit <app>/models.pypython manage.py makemigrationspython manage.py sqlmigrate <app> 0002python manage.py migrate
Fresh project
django-admin startproject mysite .python manage.py startapp pollspython manage.py migratepython manage.py createsuperuserpython manage.py runserver
Move data between databases
python manage.py dumpdata --natural-foreign --exclude contenttypes > data.json# point settings at the new DBpython manage.py migratepython manage.py loaddata data.json
CI gate
python manage.py check --deploypython manage.py makemigrations --check --dry-runpython manage.py test --parallel