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 loader
β βββ π₯ __init__.py # Exports env instance
β βββ π₯ loader.py # Pydantic BaseSettings 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 documentationDirectory 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, notifications)
- Admin interface customization
- API settings and authentication
# api/config.py structure
class DjangoCfgConfig(DjangoConfig):
# Core settings
project_name: str
debug: bool
secret_key: str
# Multi-database setup
databases: Dict[str, DatabaseConfig]
# Service configurations
email: Optional[EmailConfig]
telegram: Optional[TelegramConfig]
# Admin interface
admin_site: AdminSiteConfigSee 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 loader using pydantic-settings (BaseSettings). All configuration is loaded from environment variables and .env files β no YAML files.
loader.py - Defines pydantic BaseSettings models:
from pydantic_settings import BaseSettings, SettingsConfigDict
class EnvironmentConfig(BaseSettings):
secret_key: str = "dev-key"
debug: bool = True
# ...
model_config = SettingsConfigDict(
env_file=".env",
env_nested_delimiter="__",
)
env = EnvironmentConfig()Priority: System ENV > .env file > code defaults.
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 migrationsModels:
Post- Blog posts with title, content, status, authorComment- Comments on postsCategory- 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 migrationsModels:
Product- Products with name, price, stockOrder- Customer ordersOrderItem- 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 migrationsModels:
Profile- User profile informationUserPreferences- 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 scriptSee 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 filesCollected 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 shellpyproject.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 dependenciesManage dependencies with:
poetry add package-name
poetry install
poetry updaterequirements.txt
Pip-compatible requirements file, generated from pyproject.toml:
# Generate requirements.txt
poetry export -f requirements.txt --output requirements.txtUsed 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
API_KEY = 'xxx' # In constants.py2. 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 migrations3. 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 .env files (gitignored) or system environment variables:
# .env (development, gitignored)
DEBUG=true
DATABASE__URL="sqlite:///db/db.sqlite3"
# Production: system ENV or Docker/K8s secrets
DEBUG=false
DATABASE__URL="postgresql://..."Navigation Between Components
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:
- Start with Configuration Setup
- Review Multi-Database Setup for data layer
- Explore Admin Interface for management UI
- Check API Documentation for REST API
- Learn Background Tasks for async processing
Related Topics
- Configuration Setup - Detailed configuration guide
- Multi-Database Setup - Database routing explained
- Deployment Guide - Production deployment structure
Understanding the project structure helps you navigate and extend the sample project effectively.