I. Django Configuration with Environment Variables
Install python-dotenv
First, install the python-dotenv
package:
shellpip install python-dotenv
Configure settings.py
Add the following to your settings.py
file:
pythonfrom pathlib import Path import os from dotenv import load_dotenv # Define the base directory BASE_DIR = Path(__file__).resolve().parent.parent # Load environment variables from .env file load_dotenv(BASE_DIR / '.env') # Retrieve the secret key from the environment variables SECRET_KEY = os.getenv('SECRET_KEY') # Set the debug mode based on environment variable DEBUG = os.getenv('DEBUG') == 'True' # Configure allowed hosts ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split(' ')
Create an .env
file
In the root directory of your project, create a file named .env
with the following content:
envSECRET_KEY=your_secret_key DEBUG=True ALLOWED_HOSTS=localhost 127.0.0.1 DATABASE_URL=your_database_url
II. Handling Static Files
Install whitenoise
Install whitenoise
to serve static files efficiently:
shellpip install whitenoise
Update settings.py
Modify your settings.py
to include whitenoise
:
pythonMIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # ... ] if not DEBUG: STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
III. Database Configuration
Set Up PostgreSQL on Render
- In the Render dashboard, click "New" at the top right and select "PostgreSQL".
- Configure the PostgreSQL instance with:
- Name: Your custom name
- Database: Leave empty
- User: Leave empty
- Region: Choose the closest region
- PostgreSQL Version: 15
- Datadog API Key: Leave empty
- Plan Type: Choose according to your needs
- In the "Connections" section, note the "Internal Database URL". //dev-to-uploads.s3.amazonaws.com/uploads/articles/0tiq0cnguiybjv2rq3mj.png)
Install Required Packages
Install the necessary packages for PostgreSQL integration:
shellpip install psycopg2-binary pip install dj-database-url pip install gunicorn
Configure settings.py
Update your settings.py
for database configuration:
pythonimport dj_database_url if not DEBUG: DATABASES = { "default": dj_database_url.parse(os.getenv("DATABASE_URL")) } else: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } }
IV. Setting Up the Web Service
Configure Render Web Service
- Name: Your custom name
- Region: The same as your database
- Branch: Your main branch (e.g., main)
- Root Directory: Leave empty
- Environment: Python 3
- Build Command:
sh build.sh
- Start Command:
gunicorn core.wsgi:application
- Plan Type: Choose according to your needs
Add Environment Variables
Under the "Advanced" dropdown, add the following environment variables:
PYTHON_VERSION
: 3.9.9SECRET_KEY
: Click "Generate" to create a new keyDEBUG
: FalseALLOWED_HOSTS
: *DATABASE_URL
: Your internal database URL from Render
V. Build Script
Create build.sh
In the root directory, create a file named build.sh
with the following content:
shell#/usr/bin/env bash set -o errexit # exit on error pip install -r requirements.txt python manage.py collectstatic --no-input python manage.py migrate
This script installs the required packages, collects static files, and applies database migrations.