EgbieAndersonUku1 / Fullstack-fruit-and-veg

Fullstack Fruit & Veg Shop: A web application developed with HTML, CSS, and JavaScript for the frontend, and Django for the backend. Features include user authentication, a product catalogue, shopping cart functionality, and order management. and much more
https://fullstack-fruit-and-veg.vercel.app
0 stars 0 forks source link

Vercel Deployment Fails: Missing 'handler' or 'app' Variable in src/manage.py #1

Closed EgbieAndersonUku1 closed 2 months ago

EgbieAndersonUku1 commented 2 months ago

I am experiencing difficulties deploying my Django application to Vercel. Despite adhering closely to the Vercel documentation for Python serverless functions and Django configurations, I am encountering errors indicating that a required variable handler or app is missing. The deployment fails with the error message: "Missing variable handler or app in file src/manage.py.

Steps to Reproduce:

  1. Create a Django project with the following structure:

markdown structure


  src/
  __init__.py
  wsgi.py
    vercel-handler.py
  1. Configure vercel.json to point to src/vercel-handler.py.
  2. Deploy the project to Vercel.

vercel-handler.py:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fruit_and_veg.settings')
application = get_wsgi_application()

# Vercel requires an 'app' variable
app = application

vercel.json:


{
  "version": 2,
  "builds": [
    {
      "src": "src/vercel-handler.py",
      "use": "@vercel/python",
      "config": {
        "maxLambdaSize": "50mb"
      }
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "src/vercel-handler.py"
    }
  ]
}

manage.py:

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fruit_and_veg.settings')
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

if __name__ == '__main__':
    main()

Error Message:

Vercel Deployment Logs: Missing variable 'handler' or 'app' in file 'src/manage.py' INIT_REPORT Init Duration: 194.45 ms Phase: invoke Status: error Error Type: Runtime.ExitError

Expected Behaviour:

The Django application should be deployed and served correctly by Vercel, using vercel-handler.py as the entry point.

Actual Behaviour:

The deployment fails with an error indicating that the handler or app variable is missing, despite vercel-handler.py correctly exposing the app variable.

Additional Information:

EgbieAndersonUku1 commented 2 months ago

Refactor Project Structure and Configuration:

Moved directory to src and everything into src, so Vercel can find it Added build.sh file to streamline the build process and for Vercel to be able to build. Updated database configuration from SQLite to PostgreSQL for production readiness.