DEV Community

Thesius Code
Thesius Code

Posted on • Originally published at datanest-stores.pages.dev

Django SaaS Boilerplate

Django SaaS Boilerplate

Multi-tenant Django foundation with Stripe billing, custom user model, and production-hardened settings — launch your SaaS in days.

Python 3.11+
Django 5.0+
License: MIT


What You Get

  • Custom User model — Email-based auth with roles, built from AbstractBaseUser
  • Multi-tenant architecture — Subdomain-based tenant resolution with shared database
  • Stripe billing — Plans, subscriptions, invoices, and webhook handling
  • Production settings — Whitenoise, S3 storage, Sentry, secure cookies, HSTS
  • REST API — Django REST Framework with JWT auth and OpenAPI docs
  • Docker-ready — Gunicorn, PostgreSQL, Redis, Celery worker
  • HTMX + Alpine.js — Modern server-rendered frontend without SPA complexity
  • Split settings — Base / development / production configuration pattern

File Tree

django-saas-boilerplate/
├── config/
│   ├── settings/
│   │   ├── base.py              # Shared Django settings
│   │   ├── development.py       # Dev overrides
│   │   └── production.py        # Production hardening
│   └── urls.py                  # Root URL configuration
├── apps/
│   ├── accounts/
│   │   ├── models.py            # Custom User model
│   │   ├── serializers.py       # DRF serializers
│   │   ├── views.py             # Auth & profile ViewSets
│   │   └── urls.py              # Account routes
│   ├── tenants/
│   │   ├── models.py            # Tenant & plan models
│   │   └── middleware.py        # Subdomain → tenant
│   └── billing/
│       ├── models.py            # Subscription & Invoice
│       └── webhooks.py          # Stripe webhook handler
├── templates/
│   └── base.html                # HTMX + Alpine.js base
├── docker/
│   ├── Dockerfile               # Multi-stage Django build
│   └── docker-compose.yml       # Full stack
├── manage.py
└── manifest.json
Enter fullscreen mode Exit fullscreen mode

Getting Started

1. Clone and configure

cp .env.example .env
# Set DATABASE_URL, SECRET_KEY, STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET
Enter fullscreen mode Exit fullscreen mode

2. Run with Docker

docker compose -f docker/docker-compose.yml up -d
docker compose exec app python manage.py migrate
docker compose exec app python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

3. Or run locally

pip install -r requirements.txt
python manage.py migrate
python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

4. Create a tenant

from apps.tenants.models import Tenant, Plan

plan = Plan.objects.create(
    name="Starter",
    stripe_price_id="price_xxx",
    max_users=5,
    monthly_price_cents=2900,
)

tenant = Tenant.objects.create(
    name="Acme Corp",
    subdomain="acme",
    plan=plan,
    owner=user,
)
# Access at: https://acme.yourdomain.com
Enter fullscreen mode Exit fullscreen mode

5. Set up Stripe webhooks

# Local development with Stripe CLI
stripe listen --forward-to localhost:8000/api/v1/billing/webhook/

# Production: configure webhook URL in Stripe dashboard
# https://yourdomain.com/api/v1/billing/webhook/
Enter fullscreen mode Exit fullscreen mode

Requirements

  • Python 3.11+
  • PostgreSQL 15+
  • Redis 7+ (for Celery task queue)
  • Stripe account (for billing features)

Architecture

┌─────────────────────────────────────────────┐
│               Reverse Proxy                 │
│            (Nginx / Cloudflare)             │
└──────────────────┬──────────────────────────┘
                   │
┌──────────────────▼──────────────────────────┐
│            Django Application               │
│  ┌────────────────────────────────────────┐ │
│  │          Middleware Stack               │ │
│  │  Security → Tenant → Auth → CORS       │ │
│  └────────────────┬───────────────────────┘ │
│  ┌────────────────▼───────────────────────┐ │
│  │             URL Router                 │ │
│  │  /admin/  /api/v1/  /auth/  /docs/     │ │
│  └────────────────┬───────────────────────┘ │
│  ┌────────────────▼───────────────────────┐ │
│  │           Django Apps                  │ │
│  │  accounts │ tenants │ billing          │ │
│  └────────────────┬───────────────────────┘ │
└───────────────────┼─────────────────────────┘
      ┌─────────────┼─────────────┐
      ▼             ▼             ▼
┌──────────┐  ┌──────────┐  ┌─────────┐
│PostgreSQL│  │  Redis   │  │ Stripe  │
│  (data)  │  │ (cache/  │  │  (pay)  │
│          │  │  celery) │  │         │
└──────────┘  └──────────┘  └─────────┘
Enter fullscreen mode Exit fullscreen mode

Running Tests

python manage.py test
# or with pytest
pytest --ds=config.settings.development -v
Enter fullscreen mode Exit fullscreen mode

Related Products


This is 1 of 14 resources in the Python Developer Pro toolkit. Get the complete [Django SaaS Boilerplate] with all files, templates, and documentation for $59.

Get the Full Kit →

Or grab the entire Python Developer Pro bundle (14 products) for $159 — save 30%.

Get the Complete Bundle →


Related Articles

Top comments (0)