This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Initial setup (only needed once)
python3 -m venv venv && source venv/bin/activate && pip install uv
uv sync --all-extras # Install all dependencies
cp .env.example .env.local
# Edit .env.local with: DATABASE_URL=postgres://$(whoami)@localhost:5432/django-project-template
createdb django-project-template
uv run python manage.py migrate
uv run python manage.py createsuperuser
# Alternative: Use the setup script
./requirements/setup.sh # Installs all dev dependencies# Start development server
uv run python manage.py runserver
# Run Django shell
uv run python manage.py shell
# Make and apply migrations
uv run python manage.py makemigrations
uv run python manage.py migrate
# Collect static files
uv run python manage.py collectstatic --noinput# Run all tests
DJANGO_SETTINGS_MODULE=settings pytest
# Run specific test file
DJANGO_SETTINGS_MODULE=settings pytest apps/common/tests/test_models/test_address.py -v
# Run specific test class
DJANGO_SETTINGS_MODULE=settings pytest apps/common/tests/test_models/test_address.py::AddressModelTestCase -v
# Run specific test method
DJANGO_SETTINGS_MODULE=settings pytest apps/common/tests/test_models/test_address.py::AddressModelTestCase::test_string_representation -v
# Run tests by keyword
DJANGO_SETTINGS_MODULE=settings pytest -k "user and not stripe" -v
# Run with HTML coverage report
DJANGO_SETTINGS_MODULE=settings pytest --cov=apps --cov-report=html
# Run E2E tests
python tools/testing/browser_test_runner.py apps/**/tests/test_e2e_*.py
# Run visual tests
python tools/testing/browser_test_runner.py apps/**/tests/test_visual_*.py# Install pre-commit hooks (one-time setup)
uv run pre-commit install
uv run pre-commit install --hook-type pre-push
# Run all hooks manually
uv run pre-commit run --all-files
# Run specific hook
uv run pre-commit run black --all-files
uv run pre-commit run flake8 --all-files
# Update hook versions
uv run pre-commit autoupdate
# Skip hooks temporarily (use sparingly!)
git commit -m "message" --no-verify# Format code (use before committing)
uv run black .
uv run isort . --profile black
# Run linting
uv run flake8 . --max-line-length=88 --extend-ignore=E203,W503,E501
uv run mypy .
# Alternative: Use Ruff
uv run ruff format .
uv run ruff check . --fix
# Type checking
uv run pyright# Add production dependency
uv add package-name
# Add dev dependency
uv add --dev package-name
# Add to optional group (test, e2e)
uv add --optional test package-name
# Upgrade specific package
uv add package-name --upgrade-package package-name
# Sync dependencies
uv sync --all-extras # Install everything
uv sync # Production only
uv sync --extra dev # Production + dev
# NEVER use: pip install, uv pip install, or @latest syntaxdjango-project-template/
├── apps/ # Django applications
│ ├── common/ # Core models, behaviors, utilities
│ ├── public/ # Web UI, templates, HTMX views
│ ├── api/ # REST API endpoints
│ ├── integration/ # Third-party service integrations
│ └── ai/ # AI model integrations
├── settings/ # Modular Django settings
│ ├── __init__.py # Settings orchestrator
│ ├── env.py # Environment detection
│ ├── base.py # Core Django settings
│ ├── database.py # Database configuration
│ ├── third_party.py # External service settings
│ ├── local.py # Local development overrides
│ └── production.py # Production settings
├── static/ # All static files (no app-specific)
└── staticfiles/ # Collected static files (gitignored)
Settings are loaded in a specific order via settings/__init__.py:
- env.py - Environment detection (LOCAL, STAGE, PRODUCTION)
- base.py - Core Django configuration
- database.py - Database setup using DATABASE_URL
- third_party.py - External service configurations
- production.py - Production-specific settings (if PRODUCTION)
- local.py - Local development overrides (if LOCAL)
Environment is determined by DEPLOYMENT_TYPE in .env.local file.
- common: Foundation app with User model, behavior mixins, utilities
- public: Depends on common, provides web UI with HTMX
- api: Depends on common, provides REST endpoints
- integration: Depends on common, handles external services
- ai: Depends on common, handles AI integrations
Reusable model mixins that add common functionality:
- Timestampable:
created_at,modified_atfields - Authorable: Track content authors
- Publishable: Publishing workflow management
- Expirable: Content expiration handling
- Permalinkable: URL slug generation
- Locatable: Location/address fields
- Annotatable: Notes relationship
- ONLY use uv for all package operations
- Dependencies defined in
pyproject.toml - Python version requirement: >=3.11
- NEVER use pip directly or
uv pip install
- All templates in
apps/public/templatesdirectory - Use template inheritance with base templates
- HTMX preferred over JavaScript for interactivity
- Tailwind CSS v4 with django-tailwind-cli
- View classes:
MainContentViewfor pages,HTMXViewfor HTMX components
- Write tests BEFORE implementing features (TDD)
- Never use SQLite for tests (app uses Postgres JSON fields)
- Tests organized by type in
apps/{app_name}/tests/ - Factory classes in
apps/common/tests/factories.py - 100% coverage goal for models and behaviors
- Black formatter (line length 88)
- Type hints required for all code
- Datetime fields must end with
_at - Follow PEP 8 with Black formatting
- Group imports: stdlib, third-party, Django, local
- PostgreSQL required (JSON field support)
- Migrations: Don't run without approval
- Use behavior mixins for common model patterns
- Connection configured via DATABASE_URL
- Local settings in
.env.local(not committed) - Copy from
.env.examplefor initial setup - Key variables:
DATABASE_URL: PostgreSQL connection stringDEPLOYMENT_TYPE: LOCAL/STAGE/PRODUCTIONSECRET_KEY: Django secret keyDEBUG: True for local development
- Create model in appropriate app's
models/directory - Use behavior mixins from
apps/common/behaviors/ - Write tests first in
tests/test_models/ - Create and apply migrations
- Register in admin if needed
- Inherit from
HTMXViewinapps.public.helpers - Create partial template in
apps/public/templates/ - Handle both full page and partial responses
- Add URL pattern to app's
urls.py
- Create serializer in
apps/api/serializers/ - Create viewset/view in
apps/api/views/ - Write tests in
apps/api/tests/ - Add to
apps/api/urls.py
- Add configuration to
settings/third_party.py - Create integration module in
apps/integration/ - Define interface methods for the integration
- Keep all integration logic isolated
- You are not yet trusted to manage migrations
- Do not create or run migrations without approval
- Wait for repository owner to handle migrations
Read these documents when working on specific areas:
- ARCHITECTURE.md - System design, app relationships, data flow
- REPO_MAP.md - Complete repository structure and file organization
- MODEL_CONVENTIONS.md - Model design patterns and naming standards
- BEHAVIOR_MIXINS.md - How to use and extend behavior mixins
- HTMX_INTEGRATION.md - HTMX patterns, partial rendering, AJAX
- TAILWIND_V4.md - Tailwind CSS v4 configuration and usage
- TEMPLATE_CONVENTIONS.md - Template structure, inheritance, naming
- VIEW_CONVENTIONS.md - View patterns, MainContentView, HTMXView
- MODAL_PATTERNS.md - Modal implementation with HTMX
- ERROR_HANDLING.md - Error management strategies
- TEST_CONVENTIONS.md - Testing patterns and organization
- TEST_TROUBLESHOOTING.md - Common test issues and solutions
- guides/E2E_TESTING.md - End-to-end test implementation
- guides/BROWSER_TESTING.md - Browser automation testing
- guides/AI_BROWSER_TESTING.md - AI-assisted test generation
- guides/SETUP_GUIDE.md - Detailed setup instructions
- guides/CONTRIBUTING.md - Contribution guidelines
- guides/CICD.md - CI/CD pipeline configuration
- guides/SCREENSHOT_SERVICE.md - Visual testing service
- guides/HTMX_AND_RESPONSIVE_TESTING.md - Responsive design testing
- guides/modal_example_view.py - Modal view implementation
- guides/item_list_example.html - List view template
- guides/list_items_partial.html - HTMX partial template
- guides/example_unfold_admin.py - Admin customization
- Before starting a new feature: Read relevant architecture and convention docs
- When implementing models: Review MODEL_CONVENTIONS.md and BEHAVIOR_MIXINS.md
- For frontend work: Check HTMX_INTEGRATION.md and TEMPLATE_CONVENTIONS.md
- When writing tests: Consult TEST_CONVENTIONS.md and relevant testing docs
- For troubleshooting: Check ERROR_HANDLING.md and TEST_TROUBLESHOOTING.md
- Setting up development: Follow SETUP_GUIDE.md
- Create detailed, focused commits
- Run formatters before committing:
black . && isort . - Check changes with
git statusbefore committing - Don't include co-authors in commit messages
- do not document legacy systems
- do not document system migrations. only document new current state