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.