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

share

    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 manage.py shell

        Generate and print the SECRET_KEY:

        from django.core.management.utils import get_random_secret_key
        
        print(get_random_secret_key())

        4. Configure settings.py File:

        Update your settings.py to configure the PostgreSQL database:

          import os
          from dotenv import load_dotenv
          
          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/wsgi.py",
                    "use": "@vercel/python",
                    "config": {
                      "maxLambdaSize": "15mb",
                      "runtime": "python3.9"
                    }
                  }
                ],
                "routes": [
                  {
                    "src": "/(.*)",
                    "dest": "your_project_name/wsgi.py"
                  }
                ]
              }

              7. Vercel Project Setup

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

              8. Creating PostgreSQL Database in Vercel

              postgresql
              vercel, django
              postgresql

              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).
              vercel
              django

              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 manage.py migrate

              11. Collect Static Files

              • If you have static files, ensure they are collected properly:
              python manage.py 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 settings.py to include your Vercel domain.
              ALLOWED_HOSTS = ['your-vercel-domain.vercel.app', 'your-custom-domain.com']

              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 *

              Categories

              Trending posts

              Subscribe