Deploy Django Project On Vercel - Free Hosting Provider

A detailed step-by-step guide

Vercel, with its simplicity and a generous free tier, is an ideal hosting platform for smaller applications. It defaults to Python 3.12 as of May 11, 2024, making it crucial to understand its constraints and capabilities before diving into deployment.

Before Deployment

  1. Make sure you have updated Python to Python 3.12.

  2. Make sure you have a build_files.sh script.

    1. Create a build_files.sh in the root of your project and paste the code below:

      #!/usr/bin/env bash
      echo "Building the project..."
      python3.12 -m pip install -r requirements.txt
      echo "Collect Static..."
      python3.12 manage.py collectstatic --noinput --clear
  3. Make sure you have a requirements.txt file.

    1. Create a requirements.txt file by running the following command:

      pip freeze > requirements.txt
  4. Make sure you have a vercel.json file.

    1. Create a vercel.json in the root directory of your project and then paste the following content:

      {
          "builds": [
              {
                  "src": "yourprojectname/wsgi.py",
                  "use": "@vercel/python",
                  "config": { "maxLambdaSize": "15mb", "runtime": "python3.12" }
              },
              {
                  "src": "build_files.sh",
                  "use": "@vercel/static-build",
                  "config": { "distDir": "staticfiles_build" }
              }
          ],
          "routes": [
              {
                  "src": "/static/(.*)",
                  "dest": "/static/$1"
              },
              {
                  "src": "/(.*)",
                  "dest": "yourprojectname/wsgi.py"
              }
          ]
      }
  5. In your yourprojectname/wsgi.py file, write app=application.

  6. In your project's settings file, write:

    STATIC_URL = 'static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles_build', 'static')
    MEDIA_URLS =''
    MEDIA_ROOT = os.path.join(BASE_DIR)
    ALLOWED_HOSTS = ['*']

Production Steps

  1. Run pip install drf_yasg.
  2. Run python manage.py collectstatic.
  3. Push your code to GitHub.
  4. Login/Sign Up to Vercel and import your project.
  5. That's all! If you encounter any issues, feel free to reach out to me at jawadduet@gmail.com. I will be happy to assist you further . Happy coding!