I wanted to deploy my django app through vercel and I think I’ve done everything correctly but the deployed site gives a 404: NOT_FOUNDCode: NOT_FOUND
error. I didnt get any errors when building and my runtime logs are empty too. I’ve made sure my build_files.sh and vercel.json files are in root directory. I’ve attached necessary files below:
#settings.py
STATIC_URL = "static/"
STATICFILES_DIRS = [
BASE_DIR / "static",
]
STATIC_ROOT = os.path.join(BASE_DIR /'staticfiles','staticfiles_build','static')
ALLOWED_HOSTS = ['127.0.0.1','localhost', ".vercel.app"]
Here is my build_file
#build_files.sh
echo "BUILD START"
python3.9 -m pip install -r requirements.txt
python3.9 manage.py collectstatic --noinput --clear
echo "BUILD END"
and here is my vercel.json file
{
"version": 2,
"builds": [
{
"src": "mysite/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": "mysite/wsgi.py"
}
]
}
i’ve made sure i hit pip freeze for my requirements, i’ve also made sure my env variables are provided in the vercel settings. My database is postgresql and its currently hosted through aiven. i’ve also made sure that app = application is done in my wsgi.py inside my main app
1 Like
i’ve run into similar issues before, so let me share a few things that might help:
-
Double-check your wsgi.py file. Make sure it has both application = get_wsgi_application()
and app = application
. Sometimes Vercel can be picky about this.
-
Have you tried setting DEBUG = True
temporarily? It might give you more detailed error info to work with.
-
Your vercel.json looks good, but you could try adding “cleanUrls”: true to it. Sometimes that helps with routing issues.
-
Even though your runtime logs are empty, dig into the Vercel deployment logs if you haven’t yet.
-
This might sound silly, but double-check that your project structure matches what’s in your vercel.json. I’ve lost hours before realizing I had a typo in a stupid directory name
-
If none of that works, try a fresh deployment by running vercel --prod
from your local machine. Sometimes clearing the Vercel cache can work at times yk.
If you’re still stuck after trying these, i suggest creating a bare-bones Django project and deploying that successfully, then gradually adding your components back in. It’s a pain, but it can help pinpoint the issue.
alternatively? i noticed some issues:
-
Database configuration:
Since you’re using PostgreSQL hosted on Aiven, double-check your database settings in settings.py
. Make sure you’re using environment variables for sensitive information:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
}
}
make sure these environment variables are correctly set in Vercel’s dashboard (ik you probably have. but its worth trying)
-
Static files configuration:
Your STATIC_ROOT
setting looks unusual. Try simplifying it:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
-
build_files.sh
:
you sure this is executable? you might need to run chmod +x build_files.sh
locally before deploying.
-
Python version:
your build_files.sh
specifies Python 3.9, but make sure this matches the version in your vercel.json
and the version you’re using locally.
-
URL configuration:
shpuld check your urls.py
files (both in the project and app directories) to ensure all URLs are correctly defined.
-
Middleware:
chevk your MIDDLEWARE
setting in settings.py
. Ensure there’s no custom middleware interfering with request handling.
-
Django version:
make sure your Django version is compatible with Vercel. Try specifying the exact version in your requirements.txt
:
Django==3.2.x # Replace x with the specific version you're using
-
ALLOWED_HOSTS
:
i mean, while it looks good enough, try adding your specific Vercel app URL:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '.vercel.app', 'your-app-name.vercel.app']
-
Root URL configuration:
Ensure your project’s urls.py
includes a root URL pattern:
from django.urls import path
from your_app import views
urlpatterns = [
path('', views.home, name='home'),
# other URL patterns...
]
-
Vercel CLI:
If possible, try deploying using the Vercel CLI tool. It often provides more detailed error messages:
vercel
Follow the prompts, then run:
vercel --prod
1 Like
Thank you sir. Still with the issue, but the cli suggestion has been really helpfull.
1 Like