Deploying your Django app to Render.com is fast and easy if you follow the right steps. In this blog, Iβll walk you through setting up a production-ready Django project on Render, including database configuration, static files, and more.
π§± 1. Prepare Your Django Project
Start with a Django project that runs locally.
β Install Required Packages
Make sure you have the following in requirements.txt
:
gunicorn
dj-database-url
psycopg2-binary
whitenoise
Install them if needed:
pip install gunicorn dj-database-url psycopg2-binary whitenoise
βοΈ 2. Update Django Settings for Production
Edit your settings.py
:
π Allowed Hosts
ALLOWED_HOSTS = ['your-app-name.onrender.com']
π§Ύ Use this .env
file (You can add your other variables)
You can use a library like django-environ
to access your variables in settings.py
.
DEBUG=0
SECRET_KEY=your-complex-secret-key
ALLOWED_HOSTS=localhost,127.0.0.1,your-app-name.onrender.com
DATABASE_URL=postgres://db_user:password@hostname:PORT/db_name
ποΈ Database (PostgreSQL)
Replace the default SQLite config:
import dj_database_url
import os
DATABASES = {
'default': dj_database_url.config(
default=os.environ.get('DATABASE_URL'),
conn_max_age=600,
ssl_require=True
)
}
Render will inject the DATABASE_URL
environment variable when you connect your PostgreSQL service.
π§Ύ Static Files
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
# ... existing middleware
]
π¦ 3. Add a Procfile
in the Root Directory
Render uses this to know how to run your app:
web: gunicorn your_project_name.wsgi
Replace your_project_name
with your actual Django project folder name.
ποΈ 4. Set Up a PostgreSQL Database on Render
- Go to Render dashboard β Databases β New PostgreSQL
- Copy the generated
DATABASE_URL
- Go to your Web Service β Environment β Add:
-
DATABASE_URL
=<paste_here>
-
- Or you can directly upload your
.env
file
βοΈ 5. Set Up Static Files and Migrations
Option A: Use render.yaml
Create a render.yaml
file:
services:
- type: web
name: your-app-name
env: python
buildCommand: |
pip install -r requirements.txt
python manage.py collectstatic --noinput
python manage.py migrate
startCommand: gunicorn your_project_name.wsgi
envVars:
- key: DJANGO_SETTINGS_MODULE
value: your_project_name.settings
Push this to your GitHub repo.
Option B: Manually Run Migrations and Collectstatic
Use the Shell in Renderβs dashboard:
python manage.py migrate
python manage.py collectstatic --noinput
β 6. Final Touches
- Make sure
DEBUG = False
in production. - Use
SECRET_KEY
from an environment variable. - If you use media files (uploads), youβll need S3 or similar.
π You're Live!
After all these steps, your Django app should be live at:
https://your-app-name.onrender.com/
If the admin panel has no styles β donβt worry, just ensure:
python manage.py collectstatic --noinput
...and itβll work like a charm π
π¬ Feel free to drop a comment if you face any issues β happy to help!
Top comments (0)