Django Deployment

Complete guide to deploy your Django project to Render & Vercel

Deployment Overview

Render

Perfect for full-stack Django applications with database and static files.

  • Free tier available
  • PostgreSQL database
  • Automatic deployments
  • SSL certificates

Vercel

Optimized for frontend and serverless Django applications.

  • Edge network
  • Serverless functions
  • Fast deployments
  • GitHub integration

Prerequisites

Django Project

Working Django application ready for deployment

Git Repository

Code hosted on GitHub, GitLab, or Bitbucket

Requirements File

requirements.txt with all dependencies

Essential Files

requirements.txt

Django==4.2.7
gunicorn==21.2.0
whitenoise==6.6.0
psycopg2-binary==2.9.9
python-decouple==3.8
dj-database-url==2.1.0
django-cors-headers==4.3.1
Pillow==10.1.0
django-crispy-forms==2.1
crispy-tailwind==0.5.0

.env

SECRET_KEY=your-secret-key-here
DEBUG=False
DATABASE_URL=postgresql://user:pass@host:port/db
ALLOWED_HOSTS=yourdomain.com,.onrender.com,.vercel.app
STATIC_URL=/static/
MEDIA_URL=/media/
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_HOST_USER=your-email@gmail.com
EMAIL_HOST_PASSWORD=your-app-password

.gitignore

# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
venv/
ENV/

# Django
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
media/

# Environment variables
.env
.env.local
.env.production

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db

# Static files
/staticfiles/
/static/
collected-static/

settings.py (Key Parts)

import os
from decouple import config
import dj_database_url

# Security
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=lambda v: [s.strip() for s in v.split(',')])

# Database
DATABASES = {
    'default': dj_database_url.config(
        default=config('DATABASE_URL')
    )
}

# Static files
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

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

Deploy to Render

Step-by-step guide for Render deployment

1

Create Render Account & Connect Repository

  • • Sign up at render.com
  • • Connect your GitHub/GitLab account
  • • Select your Django project repository
2

Create Build Script

Create build.sh in your project root:

#!/usr/bin/env bash
# exit on error
set -o errexit

pip install -r requirements.txt

python manage.py collectstatic --no-input
python manage.py migrate
3

Configure Web Service

Service Settings:

  • Environment: Python 3
  • Build Command: ./build.sh
  • Start Command: gunicorn myproject.wsgi:application
  • Auto-Deploy: Yes

Environment Variables:

  • • SECRET_KEY
  • • DEBUG=False
  • • DATABASE_URL
  • • PYTHON_VERSION=3.11.6
4

Create PostgreSQL Database

  • • Go to Render Dashboard → Create → PostgreSQL
  • • Choose a name for your database
  • • Copy the External Database URL
  • • Add it as DATABASE_URL environment variable in your web service

Deploy to Vercel

Step-by-step guide for Vercel deployment

1

Create vercel.json Configuration

Create vercel.json in your project root:

{
  "version": 2,
  "builds": [
    {
      "src": "myproject/wsgi.py",
      "use": "@vercel/python",
      "config": { "maxLambdaSize": "15mb", "runtime": "python3.9" }
    },
    {
      "src": "build_files.sh",
      "use": "@vercel/static-build",
      "config": {
        "distDir": "staticfiles_build"
      }
    }
  ],
  "routes": [
    {
      "src": "/static/(.*)",
      "dest": "/static/$1"
    },
    {
      "src": "/(.*)",
      "dest": "myproject/wsgi.py"
    }
  ]
}
2

Create Build Script

Create build_files.sh:

#!/bin/bash

# Install dependencies
pip install -r requirements.txt

# Collect static files
python manage.py collectstatic --noinput --clear
3

Update Settings for Vercel

Add to your settings.py:

# Vercel specific settings
import os

# Static files
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles_build', 'static')

# Database (use external service like PlanetScale, Neon, etc.)
if 'DATABASE_URL' in os.environ:
    import dj_database_url
    DATABASES['default'] = dj_database_url.parse(os.environ['DATABASE_URL'])

# Vercel specific
if os.environ.get('VERCEL_ENV'):
    ALLOWED_HOSTS.extend(['.vercel.app', '.now.sh'])
4

Deploy to Vercel

Via Vercel CLI:

npm i -g vercel
vercel login
vercel --prod

Via Vercel Dashboard:

  • • Visit vercel.com
  • • Import Git Repository
  • • Configure environment variables
  • • Deploy

Environment Variables Setup

Render Environment Variables

SECRET_KEY

Your Django secret key

DEBUG

False

DATABASE_URL

PostgreSQL connection string

ALLOWED_HOSTS

yourdomain.com,.onrender.com

PYTHON_VERSION

3.11.6

Vercel Environment Variables

SECRET_KEY

Your Django secret key

DEBUG

False

DATABASE_URL

External database connection

ALLOWED_HOSTS

yourdomain.com,.vercel.app

Database Options

Render PostgreSQL

Built-in PostgreSQL database service

  • • Free tier: 90 days
  • • 1GB storage
  • • Automatic backups
  • • Easy integration

Neon Database

Serverless PostgreSQL for modern apps

  • • Always free tier
  • • 512MB storage
  • • Branching support
  • • Auto-scaling

PlanetScale

MySQL-compatible serverless database

  • • Free tier available
  • • Branching workflow
  • • Global replication
  • • Schema migrations

Common Issues & Solutions

Static Files Not Loading

Problem:

CSS/JS files not loading after deployment

Solution:

  • • Check STATIC_ROOT and STATIC_URL settings
  • • Run python manage.py collectstatic
  • • Ensure WhiteNoise is properly configured

Database Connection Issues

Problem:

Can't connect to database

Solution:

  • • Verify DATABASE_URL format
  • • Check database credentials
  • • Ensure psycopg2-binary is installed
  • • Run migrations after deployment

Internal Server Error (500)

Problem:

Application crashes with 500 error

Solution:

  • • Check application logs
  • • Verify all environment variables
  • • Ensure DEBUG=False in production
  • • Check ALLOWED_HOSTS setting

WSGI Configuration

wsgi.py (Production Ready)

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

application = get_wsgi_application()

# Vercel specific
app = application

urls.py (Static Files)
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    # your app urls
]

# Serve static and media files in production
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
else:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Deployment Best Practices

Security

  • • Use environment variables
  • • Set DEBUG=False
  • • Configure ALLOWED_HOSTS
  • • Use HTTPS in production

Performance

  • • Use WhiteNoise for static files
  • • Enable gzip compression
  • • Optimize database queries
  • • Use CDN for media files

Development

  • • Use version control (Git)
  • • Separate settings files
  • • Test before deployment
  • • Monitor application logs

Database

  • • Regular backups
  • • Use migrations properly
  • • Index frequently queried fields
  • • Monitor database performance

Monitoring

  • • Set up error tracking
  • • Monitor uptime
  • • Track performance metrics
  • • Set up alerts

CI/CD

  • • Automated deployments
  • • Run tests before deploy
  • • Use staging environment
  • • Rollback capabilities

Video Tutorials

Additional Resources

Quick Commands Cheat Sheet

Development Commands

# Create virtual environment
python -m venv venv
# Activate virtual environment
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Generate requirements
pip freeze > requirements.txt

Deployment Commands

# Collect static files
python manage.py collectstatic --noinput
# Run migrations
python manage.py migrate
# Create superuser
python manage.py createsuperuser
# Test production server
gunicorn myproject.wsgi:application