Django Commands

  • Learn the most important Django commands used to manage projects, apps, and databases.
  • Why Django Commands Are Important?

    Django commands help developers:

    • Create projects and apps

    • Run and test applications

    • Manage the Django workflow efficiently

    👉 Every Django developer uses these commands daily.

    What Are Django Commands?

    Django commands are built-in instructions used from the terminal/command prompt to interact with Django.

    They are executed using:

    django-admin

    or

    python manage.py

    1. startproject

    What is startproject?

    startproject is used to create a new Django project.

     It creates:

    • Project configuration files

    • Default settings

    • URL configuration

    • Entry files for the application

    When to Use?

    • When starting a new website

    • First step in Django development

    Code Example: Creating Django Project

Create Django Project

Creates a new Django project with default structure

django-admin startproject myproject
  • Output Structure

    myproject/

    ├── manage.py

    └── myproject/

        ├── settings.py

        ├── urls.py

        ├── wsgi.py

        └── asgi.py

    Recommended Way (Industry Practice)

    django-admin startproject myproject .

    ✔ Avoids nested folders
    ✔ Cleaner project structure

    2. startapp

    What is startapp?

    startapp is used to create a Django app inside a project.

    An app represents a specific feature of the website.

    Real-World Example:

    Feature

    App Name

    Login system

    accounts

    Blog

    blog

    Products

    products

    Code Example: Creating Django App

Create Django App

Generates app files and folders

python manage.py startapp myapp
  • App Structure Created

    myapp/

    ├── admin.py

    ├── apps.py

    ├── models.py

    ├── views.py

    ├── tests.py

    └── migrations/

    Important Step: Register App

    After creating an app, it must be added to settings.py.

    INSTALLED_APPS = [

        'myapp',

    ]

  • 3. runserver

    What is runserver?

    runserver starts Django’s development server.

    It allows you to:

    • Run Django locally

    • Test your application

    • See output in the browser

    Important Note:

    • This server is only for development

    • ❌ Not used in production

    Code Example: Run Django Server

Start Django Development Server

Runs Django on local machine

python manage.py runserver
  • Open Browser:

    http://127.0.0.1:8000/

    ✔ Django welcome page confirms successful setup

    Run Server on Custom Port

Custom Port Server

Runs server on port 8080

python manage.py runserver 8080
  • How These Commands Work Together (Flow)

    startproject → startapp → runserver

    1. Create project

    2. Create app

    3.  Run server

    4. Start development

    Common Beginner Mistakes (Exam & Practice)

    Mistake

    Reason

    Fix

    Command not found

    venv not active

    Activate venv

    App not showing

    Not registered

    Add to INSTALLED_APPS

    Port error

    Port busy

    Change port