CodingBowl

How to Create a Django 6 App with PostgreSQL and Bootstrap for Fly.io

Published on 14 Dec 2025 Tech Development
image
Photo by Christin Hume on Unsplash

1. Project Setup with Pipenv

Instead of manual virtual environments, Pipenv handles environment creation and dependency tracking in one step.


mkdir my_fly_project && cd my_fly_project
# Install pipenv if you don't have it: pip install pipenv
pipenv --python 3.12

2. Install Dependencies

Running these commands creates a Pipfile and Pipfile.lock. This replaces requirements.txt and ensures your production environment matches your local one exactly.


pipenv install "django>=6.0,<7.0" psycopg2-binary django-bootstrap5 python-dotenv dj-database-url gunicorn whitenoise

To activate the environment, use: pipenv shell

3. Local Configuration with .env

Pipenv automatically loads variables from a .env file into your environment when you run commands.


DEBUG=True
SECRET_KEY=your-local-dev-key
DATABASE_URL=postgres://user:pass@localhost:5432/dbname

4. Configure Django Settings

In core/settings.py, use os.getenv to pull the values Pipenv has loaded from your .env file.


import os
import dj_database_url
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG = os.getenv('DEBUG') == 'True'

ALLOWED_HOSTS = [os.getenv('FLY_APP_NAME', '') + '.fly.dev', 'localhost', '127.0.0.1']

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    # ... other middleware
]

DATABASES = {
    'default': dj_database_url.config(
        default=os.getenv('DATABASE_URL'),
        conn_max_age=600
    )
}

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

5. Bootstrap Integration

In your template (e.g., myapp/templates/base.html), use the bootstrap5 library installed via Pipenv.


{% load django_bootstrap5 %}
<!DOCTYPE html>
<html>
<head>
    {% bootstrap_css %}
</head>
<body class="container">
    <h1>Django 6 + Pipenv</h1>
    {% bootstrap_javascript %}
</body>
</html>

6. Deployment to Fly.io

Fly.io will detect your Pipfile and automatically use Pipenv to build your container.

  • fly launch: Follow the prompts to set up your app and Postgres database.
  • fly secrets set: Set your production keys (e.g., fly secrets set SECRET_KEY="prod-key").
  • fly deploy: Your app is bundled using the versions locked in Pipfile.lock.

Meow! AI Assistance Note

This post was created with the assistance of Gemini AI and ChatGPT.
It is shared for informational purposes only and is not intended to mislead, cause harm, or misrepresent facts. While efforts have been made to ensure accuracy, readers are encouraged to verify information independently. Portions of the content may not be entirely original.

image
Photo by Yibo Wei on Unsplash