Command Cheat Sheet
A quick reference for the commands used in this course. Run Django commands from the folder that contains manage.py, with the virtual environment active.
Virtual environment
| Task | macOS and Linux | Windows |
|---|
| Check Python | python3 --version | py --version |
| Create environment | python3 -m venv .venv | py -m venv .venv |
| Activate | source .venv/bin/activate | .venv\Scripts\activate |
| Deactivate | deactivate | deactivate |
After activation, python means the environment's Python on every system.
Packages
| Task | Command |
|---|
| Install Django 6.1.3 | python -m pip install "Django==6.1.3" |
| Show Django version | python -m django --version |
| Save dependencies | python -m pip freeze > requirements.txt |
| Install from file | python -m pip install -r requirements.txt |
| Upgrade pip | python -m pip install --upgrade pip |
Project commands
| Task | Command |
|---|
| Create a project here | django-admin startproject config . |
| Create an app | python manage.py startapp blog |
| Run the development server | python manage.py runserver |
| Run on another port | python manage.py runserver 8080 |
| Check for problems | python manage.py check |
| Check production readiness | python manage.py check --deploy |
Database commands
| Task | Command |
|---|
| Create migrations after a model change | python manage.py makemigrations blog |
| Apply migrations | python manage.py migrate |
| Show migration status | python manage.py showmigrations |
| Show SQL of a migration | python manage.py sqlmigrate blog 0001 |
| Open the Django shell | python manage.py shell |
| Open the database shell | python manage.py dbshell |
Users
| Task | Command |
|---|
| Create an admin account | python manage.py createsuperuser |
| Change a user's password | python manage.py changepassword USERNAME |
Tests and static files
| Task | Command |
|---|
| Run all tests | python manage.py test |
| Run one app's tests | python manage.py test blog |
| Run one test | python manage.py test blog.tests.PostViewTests.test_list_page_shows_post |
| Collect static files | python manage.py collectstatic |
Environment variables for the deployment chapter
| Task | macOS and Linux | Windows PowerShell |
|---|
| Turn debug on | export DJANGO_DEBUG=1 | $env:DJANGO_DEBUG = "1" |
| Set allowed hosts | export DJANGO_ALLOWED_HOSTS=example.com | $env:DJANGO_ALLOWED_HOSTS = "example.com" |
Template syntax
| Syntax | Purpose |
|---|
{{ variable }} | Print a value |
{% if %} and {% endif %} | Condition |
{% for x in items %} and {% endfor %} | Loop |
{% extends "blog/base.html" %} | Inherit a layout |
{% block name %} and {% endblock %} | Replaceable area |
{% url 'blog:home' %} | Reverse a URL by name |
{% load static %} and {% static 'blog/style.css' %} | Static files |
{% csrf_token %} | Form security token |
ORM quick reference
| Code | Meaning |
|---|
Post.objects.all() | All rows |
Post.objects.filter(published=True) | Matching rows |
Post.objects.exclude(published=True) | Non-matching rows |
Post.objects.get(pk=1) | Exactly one row |
Post.objects.order_by("-created") | Newest first |
Post.objects.create(...) | Create and save |
post.save() and post.delete() | Save or delete one row |
Post.objects.count() | Number of rows |
Post.objects.select_related("author") | Join a ForeignKey in one query |
Git basics
| Task | Command |
|---|
| Start a repository | git init |
| Stage everything | git add . |
| Save a snapshot | git commit -m "Finish chapter 5" |
| See changes | git status |
Official links