Django Project Structure in Django
basic · Django
A freshly generated Django project follows a standardized, modular structure designed to scale cleanly as your application grows. When you initialize a project using django-admin startproject core_gate . , Django separates your workspace into two primary layers: a Global Management Wrapper (at the root level) and a Master Configuration Capsule (contained within the project folder). Here is the exact blueprint of a production-ready Django project structure. The Enterprise Filesystem Map Plaintext my_workspace/ # The Root Project Workspace Container ├── manage.py # CLI orchestrator tool for project administration ├── db.sqlite3 # Local relational database (auto-generated on first migration) ├── .venv/ # Isolated Python virtual environment directory │ ├── core_gate/ # MASTER CONFIGURATION CAPSULE (Project Core) │ ├── __init__.py # Initializes the directory as a Python package │ ├── settings.py # Centralized settings control panel │ ├── urls.py # Global URL routing panel │ ├── wsgi.py # Synchronous WSGI web deployment interface │ └── asgi.py # Asynchronous ASGI web deployment interface │ └── positions_board/ # LOGICAL DOMAIN APP (Isolated Submodule) ├── __init__.py ├── admin.py # Panel to register models for the visual Admin console ├── apps.py # Localized application metadata configurations ├── models.py # Data Layer: Relational tables mapped as Python classes ├── views.py # Logic Layer: Processes data requests and returns actions ├── migrations/ # Auto-generated database schema version control ledger │ └── __init__.py ├── templates/ # Presentational UI Layer (HTML + DTL Layouts) │ └── positions_board/ │ └── grid.html └── urls.py # App-level localized route mapping panel Decomposing the Global Management Layer The files sitting at the absolute root of your workspace handle global project administration, environment isolation, and external deployment interfaces. manage.py (The CLI Gateway) This file is a command-line utility that wraps around Django’s administrative backend engine. You will rarely modify this file directly. Instead, you execute it via your terminal to run day-to-day configuration tasks: python manage.py runserver (Launches the local web development loop) python manage.py makemigrations (Scans models and generates schema updates) python manage.py migrate (Executes pending schema alterations directly on the database) python manage.py startapp <name> (Spawns a fresh, isolated feature submodule folder) Decomposing the Master Configuration Capsule ( core_gate/ ) This nested directory acts as the control center of your entire architecture. Every setting, security middleware, third-party plugin, and deployment pathway is managed from this folder. settings.py (The Master Control Panel) This file holds all structural configurations for the project. Key arrays and variables you will manage inside this block include: SECRET_KEY & DEBUG : Security switches. DEBUG = True handles local error logs, but must be set to False in production. ALLOWED_HOSTS : A security whitelist filtering which domain names or IP addresses are authorized to serve your backend. INSTALLED_APPS : A registry where you append your custom modular application names so Django recognizes their presence. MIDDLEWARE : A sequential execution pipeline that hooks into the global request/response lifecycle to handle CORS rules, request compression, or session cookie processing. DATABASES : Configuration dictionaries specifying connection credentials for engines like PostgreSQL, MySQL, or the local development SQLite instance. urls.py (The Root Pattern Router) This file acts as the master routing switchboard. It parses incoming HTTP request paths and maps them to their respective execution modules. For clean architecture, the root router rarely maps directly to business logic; instead, it delegates paths down to localized app-level routing files: Python # core_gate/urls.py (Master Switchboard Router) from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), # Direct link to the built-in Admin management panel # Delegating subpaths starting with /positions/ straight to the app module path('positions/', include('positions_board.urls')), ] wsgi.py & asgi.py (The Server Deployment Gateways) These files configure how your web framework communicates with production web servers. WSGI (Web Server Gateway Interface): The traditional, synchronous pipeline used to connect Django with stable deployment engines like Gunicorn or uWSGI behind an Nginx reverse proxy. ASGI (Asynchronous Server Gateway Interface): The modern, asynchronous interface enabling your project to handle concurrent persistent protocols like WebSockets or long-polling tasks via ASGI runners like Uvicorn. Decomposing the Domain Application Layer ( positions_board/ ) Django organizes feature code into Apps . An app should be a self-contained, isolated block that manages a single, focused business domain requirement. models.py : Your application's data blueprint layer. This file isolates your data tables, fields, validations, and database relationship keys using pure Python object classes. views.py : The core engine room where your business logic executes. It handles user requests, queries database records through models, processes permissions, and prepares the data context to trigger an output response. admin.py : Configuration hook file used to register local models with Django's administrative interface, instantly giving back-office users full CRUD capabilities over those database tables. migrations/ : A localized directory that functions as a git-like version control history for your database schemas. It holds the auto-generated migration steps tracking how your database tables evolve over time.