Skip to main content

Project Structure

Understanding the project structure is key to working effectively with Django-CFG. This guide provides a detailed explanation of every directory and file in the sample project.

Complete Project Treeโ€‹

my_django_cfg_demo/
โ”œโ”€โ”€ ๐Ÿ“ api/ # Configuration & Settings
โ”‚ โ”œโ”€โ”€ ๐Ÿ”ง config.py # Main Django-CFG configuration
โ”‚ โ”œโ”€โ”€ โš™๏ธ settings.py # Auto-generated Django settings
โ”‚ โ”œโ”€โ”€ ๐Ÿ”— urls.py # Root URL configuration
โ”‚ โ””โ”€โ”€ ๐Ÿ“ environment/ # Environment-specific configs
โ”‚ โ”œโ”€โ”€ ๐Ÿ› ๏ธ config.dev.yaml # Development settings
โ”‚ โ”œโ”€โ”€ ๐Ÿš€ config.prod.yaml # Production settings
โ”‚ โ”œโ”€โ”€ ๐Ÿงช config.test.yaml # Testing settings
โ”‚ โ””โ”€โ”€ ๐Ÿ“ฅ loader.py # Configuration loader
โ”œโ”€โ”€ ๐Ÿ“ apps/ # Django Applications
โ”‚ โ”œโ”€โ”€ ๐Ÿ“ blog/ # Blog with posts & comments
โ”‚ โ”œโ”€โ”€ ๐Ÿ›’ shop/ # E-commerce with products & orders
โ”‚ โ””โ”€โ”€ ๐Ÿ‘ฅ profiles/ # User profiles & preferences
โ”œโ”€โ”€ ๐Ÿ“ core/ # Core utilities
โ”‚ โ””โ”€โ”€ ๐Ÿ“ management/commands/ # Custom CLI commands
โ”œโ”€โ”€ ๐Ÿ“ db/ # Database files (SQLite)
โ”‚ โ”œโ”€โ”€ ๐Ÿ—„๏ธ db.sqlite3 # Main database
โ”‚ โ”œโ”€โ”€ ๐Ÿ“ blog.sqlite3 # Blog database (routed)
โ”‚ โ””โ”€โ”€ ๐Ÿ›’ shop.sqlite3 # Shop database (routed)
โ”œโ”€โ”€ ๐Ÿ“ docker/ # Docker configuration
โ”œโ”€โ”€ ๐Ÿ“ static/ # Static files
โ”œโ”€โ”€ ๐Ÿ“ templates/ # Django templates
โ”œโ”€โ”€ ๐ŸŽ›๏ธ manage.py # Django management script
โ”œโ”€โ”€ ๐Ÿ“‹ pyproject.toml # Poetry dependencies
โ”œโ”€โ”€ ๐Ÿ“‹ requirements.txt # Pip dependencies
โ””โ”€โ”€ ๐Ÿ“– README.md # Project documentation

Directory Breakdownโ€‹

๐Ÿ“ api/ - Configuration Hubโ€‹

The api/ directory is the heart of your Django-CFG project, containing all configuration and settings.

config.pyโ€‹

The main configuration file using Django-CFG's type-safe configuration system. This file defines:

  • Project metadata (name, debug mode, secret key)
  • Database configurations and routing rules
  • Service integrations (email, SMS, notifications)
  • Admin interface customization
  • API settings and authentication
# api/config.py structure
class SampleProjectConfig(DjangoConfig):
# Core settings
project_name: str
debug: bool
secret_key: str

# Multi-database setup
databases: Dict[str, DatabaseConfig]

# Service configurations
email: EmailConfig
twilio: TwilioConfig
telegram: TelegramConfig

# UI and API
unfold: UnfoldConfig
drf: DRFConfig

See Configuration Setup for detailed examples.

settings.pyโ€‹

Auto-generated Django settings file. This is created by Django-CFG and translates your type-safe config.py into standard Django settings.

Important: Don't edit this file directly. All changes should be made in config.py.

urls.pyโ€‹

Root URL configuration that maps URLs to views and includes:

  • Admin interface routes
  • API endpoints
  • Health check endpoints
  • Static/media file serving (development)
# api/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.api_urls')),
path('cfg/', include('django_cfg.urls')),
]

๐Ÿ“ environment/โ€‹

Environment-specific configuration files in YAML format.

config.dev.yaml - Development settings:

debug: true
is_prod: false
database:
url: "sqlite:///db/db.sqlite3"
email:
sendgrid_api_key: "" # Console backend

config.prod.yaml - Production settings:

debug: false
is_prod: true
database:
url: "postgresql://..."
email:
sendgrid_api_key: "<from-yaml-config>"

config.test.yaml - Testing settings:

debug: true
is_prod: false
database:
url: "sqlite:///:memory:"

loader.py - Configuration loader that reads YAML files and environment variables.

See Configuration Setup for environment configuration patterns.

๐Ÿ“ apps/ - Django Applicationsโ€‹

Contains all custom Django applications, following Django's app structure.

๐Ÿ“ blog/โ€‹

Blog application with posts and comments:

blog/
โ”œโ”€โ”€ models.py # Post, Comment, Category models
โ”œโ”€โ”€ admin.py # Admin interface configuration
โ”œโ”€โ”€ views.py # View logic (if any)
โ”œโ”€โ”€ serializers.py # DRF serializers for API
โ”œโ”€โ”€ urls.py # URL routing for blog
โ””โ”€โ”€ migrations/ # Database migrations

Models:

  • Post - Blog posts with title, content, status, author
  • Comment - Comments on posts
  • Category - Post categories

Database Routing: Automatically uses blog_db database.

๐Ÿ›’ shop/โ€‹

E-commerce application with products and orders:

shop/
โ”œโ”€โ”€ models.py # Product, Order, OrderItem models
โ”œโ”€โ”€ admin.py # Admin interface configuration
โ”œโ”€โ”€ views.py # View logic
โ”œโ”€โ”€ serializers.py # DRF serializers for API
โ”œโ”€โ”€ urls.py # URL routing for shop
โ””โ”€โ”€ migrations/ # Database migrations

Models:

  • Product - Products with name, price, stock
  • Order - Customer orders
  • OrderItem - Items within an order

Database Routing: Automatically uses shop_db database.

๐Ÿ‘ฅ profiles/โ€‹

User profile management:

profiles/
โ”œโ”€โ”€ models.py # Profile, Preferences models
โ”œโ”€โ”€ admin.py # Admin interface configuration
โ”œโ”€โ”€ views.py # Profile views
โ”œโ”€โ”€ serializers.py # DRF serializers
โ””โ”€โ”€ migrations/ # Database migrations

Models:

  • Profile - User profile information
  • UserPreferences - User settings and preferences

Database Routing: Uses default database (with users).

๐Ÿ“ core/ - Core Utilitiesโ€‹

Contains project-wide utilities, helpers, and custom management commands.

core/
โ”œโ”€โ”€ management/
โ”‚ โ””โ”€โ”€ commands/ # Custom CLI commands
โ”‚ โ”œโ”€โ”€ seed_data.py
โ”‚ โ””โ”€โ”€ export_data.py
โ”œโ”€โ”€ utils.py # Helper functions
โ””โ”€โ”€ middleware.py # Custom middleware (if any)

Custom commands can be run with:

python manage.py seed_data
python manage.py export_data

๐Ÿ“ db/ - Database Filesโ€‹

Contains SQLite database files for development. In production, you'll use PostgreSQL or another production database.

db/
โ”œโ”€โ”€ db.sqlite3 # Main database (auth, sessions, profiles)
โ”œโ”€โ”€ blog.sqlite3 # Blog database (posts, comments)
โ””โ”€โ”€ shop.sqlite3 # Shop database (products, orders)

Note: This directory is gitignored and only exists in development.

See Multi-Database Setup for routing and migration strategies.

๐Ÿ“ docker/ - Docker Configurationโ€‹

Docker-related files for containerization and deployment:

docker/
โ”œโ”€โ”€ Dockerfile # Main application Dockerfile
โ”œโ”€โ”€ docker-compose.yml # Multi-container setup
โ”œโ”€โ”€ nginx/
โ”‚ โ”œโ”€โ”€ nginx.conf # Nginx configuration
โ”‚ โ””โ”€โ”€ ssl/ # SSL certificates
โ””โ”€โ”€ scripts/
โ”œโ”€โ”€ entrypoint.sh # Container startup script
โ””โ”€โ”€ wait-for-it.sh # Service dependency script

See Deployment Guide for Docker setup details.

๐Ÿ“ static/ - Static Filesโ€‹

Static assets served by Django or a web server:

static/
โ”œโ”€โ”€ css/ # Stylesheets
โ”œโ”€โ”€ js/ # JavaScript files
โ”œโ”€โ”€ images/ # Images and icons
โ””โ”€โ”€ admin/ # Admin interface static files

Collected during deployment with:

python manage.py collectstatic

๐Ÿ“ templates/ - Django Templatesโ€‹

HTML templates for server-side rendering:

templates/
โ”œโ”€โ”€ base.html # Base template
โ”œโ”€โ”€ emails/ # Email templates
โ”‚ โ”œโ”€โ”€ welcome.html
โ”‚ โ””โ”€โ”€ order_confirmation.html
โ””โ”€โ”€ admin/ # Admin customizations (if any)

Key Filesโ€‹

manage.pyโ€‹

Django's command-line utility for administrative tasks:

# Common commands
python manage.py runserver # Start development server
python manage.py migrate # Run migrations
python manage.py createsuperuser # Create admin user
python manage.py shell # Interactive Python shell

pyproject.tomlโ€‹

Poetry configuration file defining project dependencies:

[tool.poetry]
name = "my-django-cfg-demo"
version = "0.1.0"

[tool.poetry.dependencies]
python = "^3.11"
django-cfg = "^1.0.0"
# ... other dependencies

Manage dependencies with:

poetry add package-name
poetry install
poetry update

requirements.txtโ€‹

Pip-compatible requirements file, generated from pyproject.toml:

# Generate requirements.txt
poetry export -f requirements.txt --output requirements.txt

Used in Docker and production environments.

README.mdโ€‹

Project documentation with:

  • Setup instructions
  • Development guidelines
  • Deployment procedures
  • Team contact information

File Organization Best Practicesโ€‹

1. Keep Configuration Centralizedโ€‹

All configuration should be in api/config.py, not scattered across the project.

# โœ… Good: Centralized configuration
class MyConfig(DjangoConfig):
email: EmailConfig = EmailConfig(...)

# โŒ Bad: Settings in multiple places
EMAIL_HOST = 'smtp.gmail.com' # In settings.py
TWILIO_SID = 'xxx' # In constants.py

2. Follow Django App Structureโ€‹

Each app should be self-contained:

app_name/
โ”œโ”€โ”€ models.py # Data models
โ”œโ”€โ”€ admin.py # Admin configuration
โ”œโ”€โ”€ views.py # View logic
โ”œโ”€โ”€ serializers.py # API serializers
โ”œโ”€โ”€ urls.py # URL routing
โ”œโ”€โ”€ tests.py # Tests
โ””โ”€โ”€ migrations/ # Database migrations

3. Use Meaningful Directory Namesโ€‹

Choose descriptive names that indicate purpose:

# โœ… Good
apps/blog/
apps/shop/
core/utils/

# โŒ Bad
app1/
stuff/
misc/

4. Separate Configuration from Codeโ€‹

Keep environment-specific settings in YAML files:

# config.dev.yaml
debug: true
database:
url: "sqlite:///db/db.sqlite3"

# config.prod.yaml
debug: false
database:
url: "postgresql://..."

The sample project components are interconnected:

  • Configuration โ†’ Used by all apps and services
  • Models โ†’ Define data structure, used by views and API
  • Admin โ†’ Manages models through web interface
  • API โ†’ Exposes models via REST endpoints
  • Background Tasks โ†’ Process async operations

To understand how these work together:

  1. Start with Configuration Setup
  2. Review Multi-Database Setup for data layer
  3. Explore Admin Interface for management UI
  4. Check API Documentation for REST API
  5. Learn Background Tasks for async processing

Understanding the project structure helps you navigate and extend the sample project effectively.