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.
- Must be run from the root directory of a Django project (where
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
.
- Includes all the commands available in
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 withpython 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
.
- Creating a new Django project:
-
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
.
- Running the development server:
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
- Create a New Project:
- Use
django-admin
to create a new project:Terminal window django-admin startproject myproject
- Use
- Navigate to the Project Directory:
- Move into the project directory:
Terminal window cd myproject
- Move into the project directory:
- Run Project-Specific Commands:
- Use
python manage.py
to manage the project:Terminal window python manage.py runserverpython manage.py migratepython manage.py createsuperuser
- Use
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
python manage.py --version
🔹 Check Available Commands
python manage.py help
🔹 Start the Development Server
python manage.py runserver
Run on a specific port:
python manage.py runserver 8080
Run on a specific IP (useful for remote access):
python manage.py runserver 0.0.0.0:8000
2️⃣ Database Management Commands
🔹 Apply Migrations
python manage.py migrate
This applies pending database migrations.
🔹 Create Migrations for Model Changes
python manage.py makemigrations
This generates migration files based on model changes.
🔹 Show SQL for Migrations (Before Running Them)
python manage.py sqlmigrate myapp 0001
Replace myapp
with your app name and 0001
with the migration number.
🔹 Check Database Issues
python manage.py check
This helps find potential issues with your project.
🔹 Reset Database (Delete & Recreate)
python manage.py flush
3️⃣ User & Authentication Management
🔹 Create a Superuser for Admin Panel
python manage.py createsuperuser
You’ll be prompted to enter a username, email, and password.
🔹 Change a User’s Password
python manage.py changepassword <username>
4️⃣ Running & Managing Applications
🔹 Create a New App
python manage.py startapp myapp
5️⃣ Shell & Data Management
🔹 Open Django Shell
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 BookBook.objects.all()
6️⃣ Static Files & Media
🔹 Collect All Static Files (For Deployment)
python manage.py collectstatic
This gathers all static files in a single directory for production use.
7️⃣ Debugging & Performance
🔹 Check for Debugging Info
python manage.py diffsettings
Shows differences between your settings and Django defaults.
🔹 Profile Database Queries
python manage.py dbshell
Allows you to run raw SQL queries inside the database shell.
8️⃣ Running Tests
🔹 Run All Tests
python manage.py test
🔹 Run Tests for a Specific App
python manage.py test myapp
🎯 Bonus: Custom Commands
You can also create custom Django management commands inside myapp/management/commands/
.