Skip to main content

Command Palette

Search for a command to run...

Django Manage.py Commands Explained with Real Use Cases

Updated
12 min read
Django Manage.py Commands Explained with Real Use Cases
C

I am Deborah, a Software Developer From Nigeria. As a Tech Enthusiast, I love to write about the latest advancements in technology. From Software Development to Cloud Computing, Cybersecurity, Cryptography and Blockchain Technology, I cover a wide range of topics in a clear and concise manner. Join me on my journey as I explore fascinating concepts in computer technology and share my insights with you. Follow my blog for regular updates and stay up-to-date on the latest tech trends!

If you have ever wanted to build a secure, data-driven website or application without starting from scratch, Django is one of the best frameworks you can turn to.

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It’s what powers platforms like Instagram, Pinterest, and even parts of Mozilla.

The concept of “building without starting from scratch” means developing applications without having to reinvent the wheel for common features like login systems, database handling, and admin panels. That means, instead of starting from scratch and figuring out everything manually—like how to structure your project, connect to a database, or validate user input—Django provides built-in tools and patterns that handle all of that for you. You still write custom code, but you don’t need to build every piece from the ground up.

In Django, you interact with the framework in two key ways: by writing Python code to build your application logic and by using the command line to manage and control the underlying framework tools. These commands, accessed through manage.py or django-admin, allow you to:

  • Create projects and apps

  • Run a development server

  • Migrate databases

  • Test your code

  • Manage users and data

  • And much more

However, many developers, especially those new to Django, tend to memorize just a few commands (runserver, makemigrations, migrate) and miss out on the broader command-line capabilities Django provides.

This guide aims to explain Django’s command-line interface and its essential commands using practical examples. By the end, you’ll know:

  • The difference between manage.py and django-admin

  • How to structure, test, and manage your Django apps using CLI tools

  • How to extend Django’s command system with custom management commands

Getting Started with Django’s command-line interface

Before discussing Django’s individual commands, it is important to understand how Django exposes them and how you, as a developer, can access and use them confidently.

django-admin vs. manage.py

Django comes with two primary command-line tools, django-admin and manage.py. These are discussed below:

  1. django-admin

    This is a global Django command-line utility. It is available once and as long as your Python environment has Django installed. You typically use it to start a new Django project. It can be thought of as Django’s master control panel.

    Example:

     django-admin startproject myproject
    
  2. manage.py

    This is a project-specific command runner. When you start a Django project using django-admin, it automatically creates a manage.py file. This file is your gateway to Django commands within that specific project.

    Example:

     python manage.py runserver
    

manage.py includes all the features of django-admin, and also adds your project’s settings and context. So unless you’re starting a project, you will usually use manage.py

How to run Django Commands

All Django commands are run in your terminal or command prompt.

The general format is:

python manage.py <command> [options]

For example:

python manage.py startapp blog
python manage.py makemigrations
python manage.py createsuperuser

If you are ever unsure what a command does, just use the help option:

python manage.py help
python manage.py help migrate

Quick tips for Beginners

If you’re using VS Code, PyCharm, or any editor with a built-in terminal, you can run these commands directly from there. No need to leave your development environment.

Categories of Django Commands

Django’s commands can be grouped into various categories based on their use cases. Understanding these categories helps you quickly locate the right command for any task you’re working on.

1) Project Setup Commands

Before you can build anything with Django, you need to initialize a project and create an app inside it. These two foundational commands — startproject and startapp — are often your first steps.

  1. django-admin startproject <projectname>

    This command creates a new Django project folder with the necessary configuration files.

    Example:

     django-admin startproject myproject
    

    What it does:

    This creates a folder structure like:

     myproject/
     ├── manage.py
     ├── myproject/
     │   ├── __init__.py
     │   ├── settings.py
     │   ├── urls.py
     │   ├── asgi.py
     │   └── wsgi.py
    

    Let us explain the above .py files (we did not create!) as follows:

    • manage.py: Used to run project specific commands

    • settings.py: Used for project configuration

    • urls.py: Used for root URL mappingssettings.py: Used for project configuration

    • urls.py: Used for root URL mappings

    • wsgi.py and asgi.py: Used to provide a standard interface between applications built with django and webservers (as deployment entry points).

Tip: Pick a lowercase, underscore-free name for the project folder — avoid names like django, test, or project to prevent conflicts.

  1. python manage.py startapp <app_name>

    Once your project is ready, you create apps within it. Django is modular, meaning each app represents a component or feature.

    Example:

     python manage.py startapp blog
    

    What it does:

    Creates an app directory structure:

     blog/
     ├── admin.py
     ├── apps.py
     ├── models.py
     ├── tests.py
     ├── views.py
     ├── urls.py         # You usually create this manually
     └── migrations/
         └── __init__.py
    

    This app now holds:

    • models.py: This holds database structure

    • views.py: This contains the logic to respond to user requests

    • admin.py: This holds the app’s admin configuration

    • tests.py: This holds unit tests for the app

Lining the App to the project:

Creating an app isn’t enough, you must tell Django to use it.

Open settings.py inside your project folder, then add the app name to INSTALLED_APPS

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        ...
        'blog',  # Add your app here
    ]

Recap Using a real-world Anology of a Company

  • A Django project is like a company.

  • Each app is like a department within that company (HR, Finance, Tech, etc.).

  • startproject sets up the company.

  • startapp creates individual departments that handle specific tasks.

2) Server and Development Shell Commands

Once you've created your Django project and app, it's time to see it in action. Django offers built-in commands to run your web server and work directly with Python and database shells, which is great for debugging and testing ideas.

  1. python manage.py runserver

    This command starts Django’s built-in development server.

    Example:

     python manage.py runserver
    

    By default, your app runs at http://127.0.0.1:8000. However, you can also specify a custom port as follows:

     python manage.py runserver 8080
    

    Or bind it to all interfaces (for local network testing):

     python manage.py runserver 0.0.0.0:8000
    

    Note: This server is only meant for development. It’s not secure for production use.

  2. python manage.py shell

    This command launches a Python shell with Django context loaded. This means you can import your models, query the database, or test logic interactively.

    Example:

     python manage.py shell
    

    From there, you can do things like

     from blog.models import Post
     Post.objects.all()
    

    This is great for testing model queries or running one-off code without needing a view or route.

  3. python manage.py dbshell

    If your database backend supports it and is configured correctly, this command opens your database shell (e.g. PostgreSQL, MySQL, SQLite).

    Example:

     python manage.py dbshell
    

    Note: This may require system-level tools like psql, mysql, or sqlite3 to be installed.

3) Database and Migration Commands

Django uses an Object-Relational Mapping (ORM) system to interact with databases. Thus, instead of writing raw SQL, you can define your models in Python, and Django turns them into database tables.

This is where migrations come in. They track changes you make to models and apply those changes to the database. Django commands relating to Database and Migration inclue the following:

  1. python manage.py makemigrations

    This scans your models and generates migration files that describe the database changes Django needs to make.

    Example:

     python manage.py makemigrations
    

    This creates a file like:

     migrations/0001_initial.py
    

    Note: You should run this every time you modify your models (e.g. add a field, remove a column).

  2. python manage.py migrate

    This command applies the migration files to the actual database, creating or updating tables based on the model definitions.

    Example:

     python manage.py migrate
    

    Use this after makemigrations to apply changes.

    Tip: The first time you run this, Django will create built-in tables like auth_user, sessions, etc.

  3. python manage.py showmigrations

    This command displays a list of all available migrations and whether they’ve been applied.

    Example:

     python manage.py showmigrations
    

    The command is helpful for tracking what changes have been made and what’s pending.

  4. python manage.py sqlmigrate <app_name> <migration_number>

    This command displays the raw SQL that Django will run for a specific migration. This is useful if you’re curious about what's happening under the hood.

    Example:

     python manage.py sqlmigrate blog 0001
    
  5. python manage.py inspectdb

    This is used to reverse

    Example:

     python manage.py inspectdb > models.py
    

    Note: The generated models may need cleanup before use

4) User Management Commands

Managing users and authentication is central to any Django project, especially for apps that require admin control or user login. Django provides command-line utilities to simplify this.

  1. python manage.py createsuperuser

    This command is used to create an administrative (superuser) account that allows access to the Django admin interface.

    Example:

     python manage.py createsuperuser
    

    You will be prompted to enter the following:

    • Username

    • Email

    • Password

Note: Superusers have full permissions and should be created only for trusted admins or developers.

  1. python manage.py changepassword <username>

    This command allows you to reset a user’s password from the command line.

    Example:

     python manage.py changepassword admin
    

    Tip: Use this command if you’ve lost access to your account or need to reset passwords without logging into the admin interface.

5) Testing and Fixtures

Testing ensures that your Django apps behave as expected. Fixtures, on the other hand, help load or dump data easily for testing or migration.

  1. Python manage.py test

    This command runs all test cases within your Django apps.

    Example:

     python manage.py test
    

    It automatically discovers files named tests.py and executes test cases using Django’s built-in testing framework (which extends Python’s unittest).

  2. python manage.py flush

    This command deletes all data from your database and resets sequences for all models.

    Example:

     python manage.py flush
    

    Note: Use this command with caution. It is destructive and often used during during early development or test resets.

  3. python manage.py loaddata <fixture_name>

    This command loads structured data i.e pre-defined data (fixtures) from JSON, XML, or YAML files into your database.

    Example:

     python manage.py loaddata users.json
    

    It is useful for seeding your database with sample or default data.

  4. python manage.py dumpdata [app.Model]

    This command exports your database records into a structured data file.

    Example:

     python manage.py dumpdata blog.Post > posts.json
    

    It is helpful for backups or data migration between environments.

6) Project Utilities

These commands are great for maintaining project health and debugging configuration issues.

  1. python manage.py check

    This command helps you check your entire project for potential problems without running migrations or starting the server.

    Example:

     python manage.py check
    

    Think of it as a “health check” command for your project.

  2. python manage.py diffsettings

    This command shows differences between your current settings and Django’s default settings.

    Example:

     python manage.py diffsettings
    

    This helps you understand which settings have been customized.

  3. python manage.py show_urls

    Note: This command requires django-extensions

    The command displays all URLs registered in your project along with their associated view functions.

    Example:

     python manage.py show_urls
    

    This is super helpful when debugging routing issues, understanding URL structure, or documenting your API endpoints.

  4. python manage.py listapps

    This command is used to list all installed apps within your Django project.

    Example:

     python manage.py listapps
    

7) Custom Command

Sometimes, you’ll want to automate repetitive tasks — like cleaning up temporary files, sending emails, or generating reports. Django lets you create custom management commands for this.

Once created, you can run them like built-in ones.

Example:

python manage.py <your_custom_command> such as:

python manage.py cleanup_tempfiles
python manage.py send_reminders

To create one:

  • Add a management/commands/ directory in your app. Inside your app, create a folder structure like this:

      your_app/
      ├── management/
      │   ├── __init__.py
      │   └── commands/
      │       ├── __init__.py
      │       └── cleanup_tempfiles.py
    
  • Write a Python file with a Command class i.e., Inside cleanup_tempfiles.py, add:

      from django.core.management.base import BaseCommand
    
      class Command(BaseCommand):
          help = 'Deletes temporary files from the system.'
    
          def handle(self, *args, **kwargs):
              # your logic here
              self.stdout.write(self.style.SUCCESS('Temporary files cleaned successfully!'))
    
  • Run it like this:

      python manage.py cleanup_tempfiles
    

Custom commands allow you to extend Django’s command ecosystem to suit your workflow.

8) Internalization

Django supports translation and localization, enabling apps to serve users in multiple languages.

  1. python manage.py makemessages -1 <lang_code>

    This command extracts translatable strings from your source code into message files.

    Example:

     python manage.py makemessages -l fr
    

    This creates a django.po file in the locale/fr/LC_MESSAGES/ directory.

  2. python manage.py compilemessages

    This command compliles the .po files into .mo files — the binary format Django uses to serve translations at runtime.

    Example

     python manage.py compilemessages
    

    Note: It is required to run this command before deploying a translated app.

9) Caching

Some commands handle caching and system-level configuration.

  1. python manage.py createcachetable

    This command creates the database table needed for database-backed caching.

    Example

     python manage.py createcachetable
    

    Note: This command is only required if your CACHES setting uses 'django.core.cache.backends.db.DatabaseCache'

10) Cleanup and Deployment

As your project grows, you will need tools for preparing it for production —handling static files, session data, and more, to manage data efficiently.

  1. python manage.py collectstatic

    This command collects all static files (CSS, JS, images) from your apps and third-party packages into one directory (usually STATIC_ROOT) for deployment.

    Example:

     python manage.py collectstatic
    

    It is necessary for production environments with Nginx or WhiteNoise.

  2. python manage.py clearsessions

    This command deletes expired session data from the database.

    Example:

     python manage.py clearsessions
    

    It is useful for reducing clutter in your DB, especially if you use django.contrib.sessions by cleaning up unnecessary data and maintaining performance.

  3. python manage.py migrate --fake

    This command is used to mark migrations as applied without actualluy running them — typically used when syncing existing databases.

    Example:

     python manage.py migrate --fake blog 0001
    

Warning: Use carefully. It can cause inconsistencies if misused.

11) Third-Party / Extension Commands

Some Django extensions and packages add their own management commands for additional capabilities.

  1. python manage.py graph_models

    (from django-extensions)

    This command generates a visual diagram of your Django models — great for documentation or team onboarding.

    Example:

     python manage.py graph_models blog -o blog_models.png
    

python manage.py runscript <script_name>

(from django-extensions)

This command allows you to run standalone scripts in your Django project context.

Example:

python manage.py runscript daily_report

This command is perfect for automation scripts like generating reports, cleaning logs, or sending notifications.

Django’s command-line interface is a like powerhouse that many developers barely scratch the surface of. From creating projects to managing users, debugging, testing, and automating workflows — these commands save time, reduce errors, and improve development efficiency.

Mastering them not only makes you faster but also deepens your understanding of how Django works under the hood. The more you explore, the more you realize how much Django does for you — allowing you to focus on what truly matters: building great applications.