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
-
Make sure you have updated Python to Python 3.12.
-
Make sure you have a
build_files.sh
script.-
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
-
-
Make sure you have a
requirements.txt
file.-
Create a
requirements.txt
file by running the following command:pip freeze > requirements.txt
-
-
Make sure you have a
vercel.json
file.-
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" } ] }
-
-
In your
yourprojectname/wsgi.py
file, writeapp=application
. -
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
- Run
pip install drf_yasg
. - Run
python manage.py collectstatic
. - Push your code to GitHub.
- Login/Sign Up to Vercel and import your project.
- 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!