Django CLI Commands Cheat Sheet
- Check the Django Version
- Command:
django-admin version - Purpose: Prints the installed Django version.
- Usage:
django-admin version
- Command:
- Start a Project
- Command:
django-admin startproject projectname [directory] - Purpose: Creates the settings package, root URLconf,
ASGI/WSGI entry points, and
manage.py. - Usage:
django-admin startproject config .
- Command:
- Create an App
- Command:
python manage.py startapp appname [directory] - Purpose: Creates an app package with models, views, tests, migrations, and app configuration.
- Usage:
python manage.py startapp blog
- Command:
- Run the Development Server
- Command:
python manage.py runserver [addrport] - Purpose: Starts Django's lightweight development server. Do not use it for production traffic.
- Usage:
python manage.py runserver python manage.py runserver 127.0.0.1:8001
- Command:
- Check Configuration and Deployment Readiness
- Command:
python manage.py check - Purpose: Runs Django's system checks. The default check does not connect to databases unless a check requires it or you pass database options.
- Usage:
python manage.py check python manage.py check --deploy --settings=config.settings.production
- Command:
- Create Database Migrations
- Command:
python manage.py makemigrations [app_label] - Purpose: Creates migration files from model changes.
- Usage:
python manage.py makemigrations python manage.py makemigrations blog
- Command:
- Inspect and Apply Migrations
- Commands:
showmigrations,migrate,sqlmigrate - Purpose: Reviews pending migrations, applies them, or displays the SQL for a named migration.
- Usage:
python manage.py showmigrations python manage.py migrate --plan python manage.py migrate python manage.py sqlmigrate blog 0001_initial
- Commands:
- Create a Superuser
- Command:
python manage.py createsuperuser - Purpose: Creates a staff user that can log into the Django admin site.
- Usage:
python manage.py createsuperuser
- Command:
- Collect Static Files
- Command:
python manage.py collectstatic - Purpose: Copies static assets from apps and
STATICFILES_DIRSintoSTATIC_ROOTfor deployment. - Usage:
python manage.py collectstatic --no-input
- Command:
- Run Tests
- Command:
python manage.py test - Purpose: Runs the project's test suite.
- Usage:
python manage.py test
- Command:
- Open the Django Shell
- Command:
python manage.py shell - Purpose: Opens an interactive shell with Django configured.
- Usage:
python manage.py shell
- Command:
- Use the Database Shell
- Command:
python manage.py dbshell - Purpose: Opens the native database client configured for
the project, such as
psql,mysql, orsqlite3. - Usage:
python manage.py dbshell
- Command:
- Load and Dump Fixtures
- Commands:
loaddataanddumpdata - Purpose: Imports or exports serialized data.
- Usage:
python manage.py loaddata seed_data python manage.py dumpdata blog --indent 2 > blog_fixture.json
- Commands:
- Create a Custom Management Command
- Location:
appname/management/commands/command_name.py - Purpose: Adds a project-specific
manage.pycommand. - Example:
from django.core.management.base import BaseCommand class Command(BaseCommand): help = 'Describe what the command does' def handle(self, *args, **options): self.stdout.write(self.style.SUCCESS('Done'))
- Location: