Comprehensive Guide to Deploying a Django Project on Vercel in under 13 steps


    1. Create a Django Project:

    Ensure you have a Django project. If not, create one using:bashCopy codedjango-admin startproject myproject cd myproject

    django-admin startproject myproject
    cd myproject

    2. Install Required Packages:

    Install necessary packages, including django, psycopg2-binary for PostgreSQL, gunicorn for the server, and python-dontenv for env files.

      pip install django gunicorn psycopg2 python-dotenv

      3. How to Generate Django Project SECRET_KEY:

      If you already have a Django project, you can use the shell command to generate a SECRET_KEY:

        python shell

        Generate and print the SECRET_KEY:

        from import get_random_secret_key

        4. Configure File:

        Update your to configure the PostgreSQL database:

          import os
          from dotenv import load_dotenv
          SECRET_KEY = os.getenv('SECRET_KEY')
          DATABASES = {
              'default': {
                  'ENGINE': 'django.db.backends.postgresql',
                  'NAME': os.getenv('DB_NAME'),
                  'USER': os.getenv('DB_USER'),
                  'PASSWORD': os.getenv('DB_PASSWORD'),
                  'HOST': os.getenv('DB_HOST'),
                  'PORT': os.getenv('DB_PORT'),

          5. Create requirements.txt:

          Generate a requirements.txt file that lists all your project’s dependencies:

            pip freeze > requirements.txt

            6. Configure vercel.json:

            Create a vercel.json file in the root of your project to define the build and deployment settings:

                "version": 2,
                "builds": [
                    "src": "your_project_name/",
                    "use": "@vercel/python",
                    "config": {
                      "maxLambdaSize": "15mb",
                      "runtime": "python3.9"
                "routes": [
                    "src": "/(.*)",
                    "dest": "your_project_name/"

              7. Vercel Project Setup

              • Install Vercel CLI:
              npm install -g vercel
              • Login to Vercel:
              vercel login
              • Deploy Your Project:

              8. Creating PostgreSQL Database in Vercel

              vercel, django

              9. Add Environment Variables in Vercel

              Go to your Vercel dashboard:

              1. Navigate to your project settings.
              2. Go to the “Environment Variables” tab.
              3. Add the environment variables defined in your .env file (SECRET_KEY, DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT).

              10. Migrate Database

              After deployment, you need to run database migrations:

              1. Open a terminal on Vercel or use Vercel’s UI to run commands.
              2. Run:
              python migrate

              11. Collect Static Files

              • If you have static files, ensure they are collected properly:
              python collectstatic

              Add this command to your deployment process in Vercel if needed.

              12. Final Check

              1. Ensure everything is working:
                • Visit your Vercel deployment URL.
                • Check logs in the Vercel dashboard for any errors.
              2. Additional Tips
                • CORS Settings: If you are using django-cors-headers, ensure you have allowed your Vercel domain in the CORS settings.
                • ALLOWED_HOSTS: Update ALLOWED_HOSTS in to include your Vercel domain.
              ALLOWED_HOSTS = ['', '']

              If you want to deploy Django Project on cPanel refer:
              On-demand Django Configuration on a Linux shared hosting account (cPanel) in just 20 Steps – Kaushal Patel

              Leave a Reply

              Your email address will not be published. Required fields are marked *


              Trending posts