Deploying Django website to Vercel
Tags: django, vercel, cloud
Published: 2024-07-10 05:36:15

Deploying a Django website to Vercel is a smart move for getting small web applications up and running quickly. Vercel is known for its simplicity. With Vercel, you get benefits like automatic SSL, serverless functions, and a globally distributed CDN etc.

This guide will walk you through the steps to deploy your Django app to Vercel, ensuring everything goes smoothly from start to finish.

## Setting up Django for Vercel deployment

Start by going to your `wsgi.py` file and add `app = application` as shown 
```py
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings') # replace with your project name.settings

application = get_wsgi_application()

app = application # add this line for vercel, as it looks for app and not application
```

Now lets add a `vercel.json` file with the following 
```py
{
    "builds": [
        {
            "src": "project/wsgi.py",
            "use": "@vercel/python",
            "config": { "maxLambdaSize": "15mb", "runtime": "python3.12" }
          },
          {
            "src": "build_files.sh",
            "use": "@vercel/static-build",
            "config": {
              "distDir": "staticfiles"
            }
          }
    ],
    "routes": [
        {
            "src": "/static/(.*)",
            "dest": "/static/$1"
          },
        {
        "src": "/(.*)",
        "dest": "project/wsgi.py"
        }
    ]

}
```
make sure to replace `"staticfiles"` with your own static files directory and the build's src to your project name.

Create a `build_files.sh` and add the following
```bash
#!/bin/bash
python3 -m pip install -r requirements.txt
python3 manage.py collectstatic --noinput
```

## Deploying to vercel
Once you have signed up to vercel, go ahead and create a project

![Create a project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et8m5sly5na9dt4gvucs.png)

Now import repository, if you don't find it then adjust the gihub app permissions.

![upload code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bd0har41yxj4zwv4o8o6.png)

Now give it a project name and click on deploy

![project name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rf83qji111oyuqm5rhe3.png)

Make sure to set `DEBUG=False` in `settings.py`, you may see an error asking you to add domain to allowed host, so add it and deploy it again.

That's it your deployment is ready!

## Environment variables
If you are using env variables, You can add environment variables by going to settings -> environment variables

![env-variable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d6w9375g06zxrzlfaax.png)

then go to deployments and click on redeploy

![redeploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64tx69kvjhdp7ry3alrv.png)

## Debugging
If you are getting errors, click on the deployment and check the build logs to correct the error

![build logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgrowkyib3gbqb6r60el.png)

That's it, if you found it helpful make sure to share the article. If you want to learn more about django production tips follow.