Next

Django Project Structure

  • Understand the Django project structure and important files used in a Django application.
  • Why Project Structure Is Important?

    Understanding Django’s project structure helps students:

    • Know where to write code

    • Understand how Django works internally

    • Debug errors easily

    • Work confidently in real projects

    👉 Every professional Django developer must master this lesson.

    Default Django Project Structure

    myproject/

    ├── manage.py

    └── myproject/

        ├── __init__.py

        ├── settings.py

        ├── urls.py

        ├── asgi.py

        └── wsgi.py

    1. manage.py

    What is manage.py?

    manage.py is a command-line utility used to:

    • Run the server

    • Create apps

    • Apply migrations

    • Access Django shell

    • Perform admin tasks

    Common Commands Using manage.py

    Command

    Purpose

    runserver

    Start development server

    startapp

    Create new app

    makemigrations

    Create DB changes

    migrate

    Apply DB changes

    createsuperuser

    Admin user

    Code Example: Running Django Server

Using manage.py

Starts Django development server

python manage.py runserver
  • Behind the Scenes:

    • Loads settings.py

    • Initializes Django

    • Starts HTTP server

    2. settings.py

    What is settings.py?

    settings.py is the main configuration file of the project.

    It controls:

    • Installed apps

    • Database settings

    • Templates

    • Middleware

    • Security settings

    Important Settings Explained

    INSTALLED_APPS

    INSTALLED_APPS = [

        'django.contrib.admin',

        'django.contrib.auth',

        'django.contrib.sessions',

    ]


    👉 Registers apps in the project

    DATABASES

    DATABASES = {

        'default': {

            'ENGINE': 'django.db.backends.sqlite3',

            'NAME': BASE_DIR / 'db.sqlite3',

        }

    }


    👉 Defines database connection

    TEMPLATES

    TEMPLATES = [

        {

            'BACKEND': 'django.template.backends.django.DjangoTemplates',

            'DIRS': [],

        },

    ]

    👉 Controls HTML templates

    Code Example: Adding an App in settings.py

Register Django App

Enables app in the project

INSTALLED_APPS = [
    'myapp',
]

  • 3. urls.py

    What is urls.py?

    urls.py handles URL routing.

    👉 It connects:
    URL → View → Response

    Responsibilities:

    • Map URLs to views

    • Include app URLs

    • Control website navigation

    Code Example: Basic URL Configuration

URL Mapping

Connects URL with view function

from django.urls import path
from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to Django")

urlpatterns = [
    path('', home),
]

Including App URLs (Real Project Use)

from django.urls import path, include

urlpatterns = [
    path('blog/', include('blog.urls')),
]
  • 4. wsgi.py

    What is wsgi.py?

    WSGI (Web Server Gateway Interface):

    • Used for synchronous web servers

    • Required for production deployment

    👉 Connects Django with servers like:

    • Gunicorn

    • uWSGI

    • Apache

    Simple Explanation:

    WSGI allows Django to talk to web servers.

    Code Example: wsgi.py File

WSGI Configuration

Entry point for production servers

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = get_wsgi_application()
  • 5. asgi.py

    What is asgi.py?

    ASGI (Asynchronous Server Gateway Interface):

    • Supports async features

    • Used for:

      • WebSockets

      • Chat apps

      • Real-time apps

    Difference Between WSGI & ASGI

    Feature

    WSGI

    ASGI

    Type

    Sync

    Async

    WebSockets

    Real-time apps


    Code Example: asgi.py File

ASGI Configuration

Enables async capabilities

import os
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = get_asgi_application()
  • 6. WSGI vs ASGI (Interview Important)

    Aspect

    WSGI

    ASGI

    Django version

    All

    Django 3.0+

    Speed

    Moderate

    High

    Use-case

    Traditional websites

    Real-time apps

Next