Deploying Django app on Vercel

5 min read

Deploying Django app on Vercel

Vercel is best known for hosting frontend frameworks like Next.js, but with the rise of serverless architectures, it’s now possible to deploy Django applications on Vercel too. In this guide, we’ll walk through step-by-step how to deploy a Django app using Vercel’s serverless functions.

Prerequisites

  • Python 3.10 or later
  • Django installed locally
  • Git and Github
  • A Vercel account
  • vercel CLI installed globally:
npm install -g vercel

Step 1: Prepare Your Django App

Start by creating a Django project if you don’t already have one:

django-admin startproject myproject
cd myproject

Run the app locally to confirm it works:

python manage.py runserver

If it runs successfully at http://127.0.0.1:8000/, you’re ready to continue.

Step 2: Setup Database (NeonDB + dj-database-url)

Vercel doesn’t offer a built-in database, so we’ll use NeonDB — a modern, serverless PostgreSQL provider. We’ll connect it to Django using dj-database-url and psycopg2-binary.

Create a NeonDB Account and Database:

  1. Go to https://neon.tech and sign up.
  2. Create a new project → choose PostgreSQL.
  3. Copy your connection string. It will look like this:
postgres://user:password@ep-xxxxxx.ap-southeast-1.aws.neon.tech/neondb
  1. Install required packages
pip install dj-database-url psycopg2-binary
  1. Export depedencies
pip freeze > requirements.txt

Configure Database in Django Open myproject/settings.py and modify your database settings:

import os
import dj_database_url

DATABASES = {
    "default": dj_database_url.config(
        default=os.environ.get("DATABASE_URL"),
        conn_max_age=600,
        ssl_require=True
    )
}

This tells Django to read the connection string from an environment variable called DATABASE_URL.

Add the Database URL to Vercel Once deployed, you’ll need to add your NeonDB connection URL to Vercel.

vercel env add DATABASE_URL

Paste your NeonDB connection string when prompted.

For local development, you can store it in a .env file (optional) and use django-environ to load it.

Apply migrations Run the following commands locally to apply your migrations:

python manage.py migrate

If successful, your Django app is now connected to NeonDB. 🚀

Step 3: Project Structure for Vercel

Vercel expects a specific layout for Python-based serverless functions. Your final structure should look like this:

myproject/

├── manage.py
├── myproject/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py

├── requirements.txt
└── vercel.json

Rename myproject/myproject folder to myproject/api, so it will become like:

myproject/

├── manage.py
├── api/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py

├── requirements.txt
└── vercel.json

Important! Do not forget to also change your settings:

ROOT_URLCONF = 'myproject.urls'
WSGI_APPLICATION = 'myproject.wsgi.application'

# change to:
ROOT_URLCONF = 'api.urls'
WSGI_APPLICATION = 'api.wsgi.application'

Also change myproject.settings everywhere to api.settings:

# myproject/api/asgi.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
# change to:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
# myproject/api/wsgi.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
# change to:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
# myproject/manage.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
# change to:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')

After all set, create a new file named vercel.json in the project root:

{
 "routes": [
   {
     "src": "/(.*)",
     "dest": "api/wsgi.py"
   }
 ]
}

This configuration tells Vercel to use Django’s WSGI entry point and handle all incoming routes through it.

Step 4: Adjust settings.py

To make Django compatible with serverless environments, update the following in your settings.py:

import os

DEBUG = os.environ.get("DEBUG", "False") == "True"
ALLOWED_HOSTS = ["127.0.0.1", ".vercel.app"]

If you also use dj-rest-auth and JWT Cookie-Based auth then add this to your settings:

CSRF_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = 'None'
CSRF_COOKIE_DOMAIN = '*.vercel.app'
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_SAMESITE = 'None'
SESSION_COOKIE_DOMAIN = '*.vercel.app'

REST_AUTH = {
    'SESSION_LOGIN': True,
    'USE_JWT': True,
    'JWT_AUTH_SECURE': True,
    'JWT_AUTH_HTTPONLY': True,
    'JWT_AUTH_SAMESITE': 'None',
    'JWT_AUTH_RETURN_EXPIRATION': False,
    'JWT_AUTH_COOKIE_USE_CSRF': False,
    'JWT_AUTH_COOKIE_ENFORCE_CSRF_ON_UNAUTHENTICATED': False,
}

Step 5: Set Up Environment Variables

Vercel lets you manage environment variables securely. In your terminal:

vercel env add SECRET_KEY
vercel env add DEBUG

You’ll be prompted to enter values. For example:

? What’s the value of SECRET_KEY? my-super-secret-key
? What’s the value of DEBUG? False

Step 6: Deploy to Vercel

Now, initialize and deploy:

vercel

The CLI will ask:

  • Link to existing project? → No
  • Project name? → myproject
  • Which directory? → .
  • Want to override settings? → No

Once complete, Vercel will build your Django app as a serverless function and output a live deployment URL like:

https://myproject.vercel.app

Conclusion

You’ve successfully deployed a Django app on Vercel! 🎉 With NeonDB as your PostgreSQL backend and Django running on Vercel’s serverless runtime, you have a scalable, modern stack — ideal for lightweight APIs, admin dashboards, or hobby projects.

If you need background jobs, file storage, or persistent caching, pair this setup with external services like AWS Lambda, Cloudflare R2, or Redis Cloud.

References