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

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

Building a Django 6 application with a PostgreSQL database is a professional-grade setup that ensures scalability. This guide covers using a requirements.txt file for dependency management, Bootstrap for the frontend, and environment variables for security.

1. Project Setup & Virtual Environment

First, create your project directory and set up a virtual environment to keep your dependencies isolated.


mkdir my_fly_project && cd my_fly_project
python -m venv venv
# Activate on Windows: venv\Scripts\activate
# Activate on Mac/Linux: source venv/bin/activate

2. Define Dependencies (requirements.txt)

Fly.io requires a production-grade web server like Gunicorn and the dj-database-url package to parse the database connection string.


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

Install these using: pip install -r requirements.txt

3. Local Configuration with .env

Create a file named .env in your root folder. This keeps sensitive data out of your source code.


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

4. Configure Django Settings

Modify core/settings.py to load the environment variables and configure Whitenoise for static files (Bootstrap).


import os
import dj_database_url
from pathlib import Path
from dotenv import load_dotenv

load_dotenv()
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 file (e.g., myapp/templates/base.html), use the bootstrap5 tags to load assets.


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

6. Deployment to Fly.io

Follow these steps to push your application live:

  • fly launch: Initialize the app and create a PostgreSQL database when prompted.
  • fly secrets set: Upload your production SECRET_KEY and set DEBUG=False.
  • fly deploy: Build and release your application.

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