Skip to content

2. Command Utilities

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:

  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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. 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

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:

Terminal window
python manage.py --version
Terminal window
python manage.py help
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
Terminal window
python manage.py migrate

This applies pending database migrations.

Terminal window
python manage.py makemigrations

This generates migration files based on model changes.

🔹 Show SQL for Migrations (Before Running Them)

Section titled “🔹 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.

Terminal window
python manage.py check

This helps find potential issues with your project.

Terminal window
python manage.py flush
Terminal window
python manage.py createsuperuser

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

Terminal window
python manage.py changepassword <username>
Terminal window
python manage.py startapp myapp
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()

🔹 Collect All Static Files (For Deployment)

Section titled “🔹 Collect All Static Files (For Deployment)”
Terminal window
python manage.py collectstatic

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

Terminal window
python manage.py diffsettings

Shows differences between your settings and Django defaults.

Terminal window
python manage.py dbshell

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

Terminal window
python manage.py test
Terminal window
python manage.py test myapp

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