Use Django 4 on Vercel with Serverless Functions using the Python Runtime.
This example shows how to use Django 4 on Vercel with Serverless Functions using the Python Runtime.
https://django-template.vercel.app/
Our Django application, example
is configured as an installed application in api/settings.py
:
# api/settings.pyINSTALLED_APPS = [# ...'example',]
We allow "*.vercel.app" subdomains in ALLOWED_HOSTS
, in addition to 127.0.0.1:
# api/settings.pyALLOWED_HOSTS = ['127.0.0.1', '.vercel.app']
The wsgi
module must use a public variable named app
to expose the WSGI application:
# api/wsgi.pyapp = get_wsgi_application()
The corresponding WSGI_APPLICATION
setting is configured to use the app
variable from the api.wsgi
module:
# api/settings.pyWSGI_APPLICATION = 'api.wsgi.app'
There is a single view which renders the current time in example/views.py
:
# example/views.pyfrom datetime import datetimefrom django.http import HttpResponsedef index(request):now = datetime.now()html = f'''<html><body><h1>Hello from Vercel!</h1><p>The current time is { now }.</p></body></html>'''return HttpResponse(html)
This view is exposed a URL through example/urls.py
:
# example/urls.pyfrom django.urls import pathfrom example.views import indexurlpatterns = [path('', index),]
Finally, it's made accessible to the Django server inside api/urls.py
:
# api/urls.pyfrom django.urls import path, includeurlpatterns = [...path('', include('example.urls')),]
This example uses the Web Server Gateway Interface (WSGI) with Django to enable handling requests on Vercel with Serverless Functions.
python manage.py runserver
Your Django application is now available at http://localhost:8000
.
Deploy the example using Vercel:
Use Django 4 on Vercel with Serverless Functions using the Python Runtime.
This example shows how to use Django 4 on Vercel with Serverless Functions using the Python Runtime.
https://django-template.vercel.app/
Our Django application, example
is configured as an installed application in api/settings.py
:
# api/settings.pyINSTALLED_APPS = [# ...'example',]
We allow "*.vercel.app" subdomains in ALLOWED_HOSTS
, in addition to 127.0.0.1:
# api/settings.pyALLOWED_HOSTS = ['127.0.0.1', '.vercel.app']
The wsgi
module must use a public variable named app
to expose the WSGI application:
# api/wsgi.pyapp = get_wsgi_application()
The corresponding WSGI_APPLICATION
setting is configured to use the app
variable from the api.wsgi
module:
# api/settings.pyWSGI_APPLICATION = 'api.wsgi.app'
There is a single view which renders the current time in example/views.py
:
# example/views.pyfrom datetime import datetimefrom django.http import HttpResponsedef index(request):now = datetime.now()html = f'''<html><body><h1>Hello from Vercel!</h1><p>The current time is { now }.</p></body></html>'''return HttpResponse(html)
This view is exposed a URL through example/urls.py
:
# example/urls.pyfrom django.urls import pathfrom example.views import indexurlpatterns = [path('', index),]
Finally, it's made accessible to the Django server inside api/urls.py
:
# api/urls.pyfrom django.urls import path, includeurlpatterns = [...path('', include('example.urls')),]
This example uses the Web Server Gateway Interface (WSGI) with Django to enable handling requests on Vercel with Serverless Functions.
python manage.py runserver
Your Django application is now available at http://localhost:8000
.
Deploy the example using Vercel: