Skip to content

2. Command Utilities

1. django-admin Command Utility

django-admin and python manage.py are both command-line utilities used in Django development, but they serve slightly different purposes and are used in different contexts. Here’s a detailed comparison:

1.1 Purpose and Scope

  • django-admin:

    • A global command-line utility installed with Django.
    • Used for tasks that are not specific to a particular Django project.
    • Can be used to create new projects, manage settings, and perform general Django-related tasks.
  • python manage.py:

    • A project-specific command-line utility automatically generated when you create a Django project.
    • Used for tasks that are specific to the Django project in which it resides.
    • Extends django-admin by adding project-specific settings and custom management commands.

1.2 Usage Context

  • django-admin:

    • Can be run from anywhere on your system.
    • Does not require access to a specific Django project.
    • Useful for creating new projects or performing global Django tasks.
  • python manage.py:

    • Must be run from the root directory of a Django project (where manage.py is located).
    • Requires access to the project’s settings and configuration.
    • Used for managing and interacting with a specific Django project.

1.3 Commands Available

  • django-admin:

    • Provides a set of core commands that are not tied to any specific project.
    • Examples: startproject, startapp, check, version.
  • python manage.py:

    • Includes all the commands available in django-admin, but with project-specific settings preloaded.
    • Also includes custom management commands defined in your project’s apps.
    • Examples: runserver, migrate, createsuperuser, collectstatic.

1.4 Project-Specific Settings

  • django-admin:

    • Does not automatically load project-specific settings.
    • Requires you to explicitly specify settings using the --settings flag if needed.
  • python manage.py:

    • Automatically loads the settings module for the project where it is located.
    • No need to specify settings manually.

1.5 Custom Management Commands

  • django-admin:

    • Cannot directly run custom management commands defined in your project’s apps.
  • python manage.py:

    • Can run custom management commands defined in your project’s apps.
    • Example: If you create a custom command mycommand in your app, you can run it with python manage.py mycommand.

1.6 Common Use Cases

  • django-admin:

    • Creating a new Django project: django-admin startproject myproject.
    • Creating a new app: django-admin startapp myapp.
    • Checking Django version: django-admin version.
    • Running system checks: django-admin check.
  • python manage.py:

    • Running the development server: python manage.py runserver.
    • Applying database migrations: python manage.py migrate.
    • Creating a superuser: python manage.py createsuperuser.
    • Collecting static files: python manage.py collectstatic.
    • Running tests: python manage.py test.

1.7 Flexibility

  • django-admin:

    • More flexible for global tasks and initial project setup.
    • Can be used without a specific project context.
  • python manage.py:

    • More flexible for project-specific tasks and custom commands.
    • Requires a project context to function.

1.8 Example Workflow

  1. Create a New Project:
    • Use django-admin to create a new project:
      Terminal window
      django-admin startproject myproject
  2. Navigate to the Project Directory:
    • Move into the project directory:
      Terminal window
      cd myproject
  3. Run Project-Specific Commands:
    • Use python manage.py to manage the project:
      Terminal window
      python manage.py runserver
      python manage.py migrate
      python manage.py createsuperuser

2. manage.py Command Utility

The manage.py script in Django is a powerful command-line utility that helps you manage your Django project. Below are some essential and useful manage.py commands:

1️⃣ Basic Commands

🔹 Check Django Version

Terminal window
python manage.py --version

🔹 Check Available Commands

Terminal window
python manage.py help

🔹 Start the Development Server

Terminal window
python manage.py runserver

Run on a specific port:

Terminal window
python manage.py runserver 8080

Run on a specific IP (useful for remote access):

Terminal window
python manage.py runserver 0.0.0.0:8000

2️⃣ Database Management Commands

🔹 Apply Migrations

Terminal window
python manage.py migrate

This applies pending database migrations.

🔹 Create Migrations for Model Changes

Terminal window
python manage.py makemigrations

This generates migration files based on model changes.

🔹 Show SQL for Migrations (Before Running Them)

Terminal window
python manage.py sqlmigrate myapp 0001

Replace myapp with your app name and 0001 with the migration number.

🔹 Check Database Issues

Terminal window
python manage.py check

This helps find potential issues with your project.

🔹 Reset Database (Delete & Recreate)

Terminal window
python manage.py flush

3️⃣ User & Authentication Management

🔹 Create a Superuser for Admin Panel

Terminal window
python manage.py createsuperuser

You’ll be prompted to enter a username, email, and password.

🔹 Change a User’s Password

Terminal window
python manage.py changepassword <username>

4️⃣ Running & Managing Applications

🔹 Create a New App

Terminal window
python manage.py startapp myapp

5️⃣ Shell & Data Management

🔹 Open Django Shell

Terminal window
python manage.py shell

This allows you to interact with Django models and database from Python.

Example usage inside the shell:

from myapp.models import Book
Book.objects.all()

6️⃣ Static Files & Media

🔹 Collect All Static Files (For Deployment)

Terminal window
python manage.py collectstatic

This gathers all static files in a single directory for production use.

7️⃣ Debugging & Performance

🔹 Check for Debugging Info

Terminal window
python manage.py diffsettings

Shows differences between your settings and Django defaults.

🔹 Profile Database Queries

Terminal window
python manage.py dbshell

Allows you to run raw SQL queries inside the database shell.

8️⃣ Running Tests

🔹 Run All Tests

Terminal window
python manage.py test

🔹 Run Tests for a Specific App

Terminal window
python manage.py test myapp

🎯 Bonus: Custom Commands

You can also create custom Django management commands inside myapp/management/commands/.