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
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
{
"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
#!/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
Now import repository, if you don't find it then adjust the gihub app permissions.
Now give it a project name and click on deploy
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
then go to deployments and click on redeploy
Debugging
If you are getting errors, click on the deployment and check the build logs to correct the error
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.
Top comments (0)