Deploying a Django Project on Vercel

Prerequisites

Steps

  1. Install the required Python packages:
    pip install psycopg2-binary dj-database-url python-decouple
  2. Update your requirements.txt file:
    pip freeze > requirements.txt
  3. Create a .env file in the root directory of your Django project and add the following line:
    DATABASE_URL="{your cloud database url}"
  4. Create a .gitignore file in the root directory of your Django project and add the following lines:
    *.env
    bin/
    lib/
    *.sqlite3
  5. Create a build.sh file in the root directory of your Django project and add the following contents:
    #!/bin/bash
    
    # Build the project
    echo "Building the project..."
    python3.9 -m pip install -r requirements.txt
    
    echo "Make Migration..."
    python3.9 manage.py makemigrations --noinput
    python3.9 manage.py migrate --noinput
    
    echo "Collect Static..."
    python3.9 manage.py collectstatic --noinput
  6. Create a vercel.json file in the root directory of your Django project and add the following contents:
    {
      "version": 2,
      "builds": [
        {
          "src": "backend/wsgi.py",
          "use": "@vercel/python",
          "config": {
            "maxLambdaSize": "15mb",
            "runtime": "python3.9"
          }
        },
        {
          "src": "build.sh",
          "use": "@vercel/static-build",
          "config": {
            "distDir": "staticfiles_build"
          }
        }
      ],
      "routes": [
        {
          "src": "/static/(.*)",
          "dest": "/static/$1"
        },
        {
          "src": "/(.*)",
          "dest": "backend/wsgi.py"
        }
      ]
    }
  7. Make the following changes to your Django project settings:

    settings.py

    
    import dj_database_url
    from decouple import config
    
    # Change the DATABASES configuration
    DATABASES["default"] = dj_database_url.config()
    
    # Add these for static files
    STATIC_URL = 'static/'
    STATIC_ROOT = BASE_DIR / "staticfiles_build" / "static"
    
    # Add these for media files
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    
  8. In your project's wsgi.py file, change the application variable to 'app':

    wsgi.py

    app = get_wsgi_application()
  9. Upload your project to GitHub.
  10. Import your GitHub repository into Vercel and deploy your project.
  11. Access your deployed Django project on Vercel.

Conclusion

Congratulations! You've successfully deployed your Django project on Vercel. Now your project is accessible to the world.

Website Project

Building engaging websites that naturally incorporate the power of artificial intelligence and other innovative technologies is where my expertise shines. I design and develop websites that effortlessly fuse creativity, intelligence, and user-centric functionality. With a harmonious blend of art and technology, my digital creations offer a seamless and captivating online experience.