This guide provides step-by-step instructions to deploy a Django project on Vercel. Follow these steps to get your Django application up and running on Vercel.
- Python and Django installed
- Vercel account
- GitHub account
- A Django project ready for deployment
Generate a requirements.txt
file containing all the dependencies required by your project.
pip freeze > requirements.txt
Create a file named build_files.sh
in the same directory as your manage.py
file and add the following code:
# build_files.sh
pip install -r requirements.txt
python3.9 manage.py collectstatic --noinput
Create a vercel.json
file in the same directory as your manage.py
file and add the following code:
{
"version": 2,
"builds": [
{
"src": "DjangoWebScraping/wsgi.py",
"use": "@vercel/python",
"config": { "maxLambdaSize": "15mb", "runtime": "python3.9" }
},
{
"src": "build_files.sh",
"use": "@vercel/static-build",
"config": {
"distDir": "staticfiles_build"
}
}
],
"routes": [
{
"src": "/static/(.*)",
"dest": "/static/$1"
},
{
"src": "/(.*)",
"dest": "DjangoWebScraping/wsgi.py"
}
]
}
Note: Change the
src
anddest
according to the path of yourwsgi.py
file.
Add the following line at the end of your wsgi.py
file:
app = application
Make the following changes in your settings.py
file:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles_build', 'static')
ALLOWED_HOSTS = ['127.0.0.1', '.vercel.app', '.now.sh']
Create a repository on GitHub and push your project to the repository. You can create a new repository directly on GitHub or use the command line.
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin <your-repo-URL>
git push -u origin main
- Go to Vercel and log in with your GitHub account.
- Click on "Add New" then "Project".
- Select your GitHub repository.
- Name your deployment and configure your project settings if needed (such as the root directory).
- Click "Deploy".
After following these steps, your Django project should be successfully deployed on Vercel. Visit your Vercel dashboard to manage and view your project.
Abhishek Rajput