Django CLI Commands Cheat Sheet

  1. Check the Django Version
    • Command: django-admin version
    • Purpose: Prints the installed Django version.
    • Usage:
      django-admin version
  2. 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 .
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Collect Static Files
    • Command: python manage.py collectstatic
    • Purpose: Copies static assets from apps and STATICFILES_DIRS into STATIC_ROOT for deployment.
    • Usage:
      python manage.py collectstatic --no-input
  10. Run Tests
    • Command: python manage.py test
    • Purpose: Runs the project's test suite.
    • Usage:
      python manage.py test
  11. Open the Django Shell
    • Command: python manage.py shell
    • Purpose: Opens an interactive shell with Django configured.
    • Usage:
      python manage.py shell
  12. Use the Database Shell
    • Command: python manage.py dbshell
    • Purpose: Opens the native database client configured for the project, such as psql, mysql, or sqlite3.
    • Usage:
      python manage.py dbshell
  13. Load and Dump Fixtures
    • Commands: loaddata and dumpdata
    • Purpose: Imports or exports serialized data.
    • Usage:
      python manage.py loaddata seed_data
      python manage.py dumpdata blog --indent 2 > blog_fixture.json
  14. Create a Custom Management Command
    • Location: appname/management/commands/command_name.py
    • Purpose: Adds a project-specific manage.py command.
    • 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'))