Deploy a Django app on Vercel
Deploy a Django app to Vercel with the Python runtime and Vercel Functions.
Vercel detects manage.py and reads your WSGI or ASGI entrypoint from your
project settings.
Create a Django project or use an existing one:
Initialize a new Django project with the Vercel CLI init command:
vc initdjangoThis clones the Django example repository into a directory called django.
Vercel automatically detects Django projects by locating manage.py in your repository. Vercel then executes manage.py to discover your DJANGO_SETTINGS_MODULE and determines the entrypoint from WSGI_APPLICATION or ASGI_APPLICATION.
For a WSGI app (the default), configure your settings and wsgi.py:
WSGI_APPLICATION ='myproject.wsgi.application'import osfrom django.core.wsgi import get_wsgi_applicationos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')application =get_wsgi_application()For an ASGI app, set ASGI_APPLICATION instead and define application in asgi.py:
ASGI_APPLICATION ='myproject.asgi.application'import osfrom django.core.asgi import get_asgi_applicationos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')application =get_asgi_application()To point Vercel to a Django app in a custom module, set tool.vercel.entrypoint in pyproject.toml:
[tool.vercel]entrypoint ="myproject.wsgi:application"The tool.vercel.entrypoint value tells Vercel to look for a WSGI instance named application in ./myproject/wsgi.py.
The build property in [tool.vercel.scripts] defines the Build Command for Django deployments. It runs after dependencies are installed and before Vercel deploys your application:
[tool.vercel.scripts]build ="python build.py"For example:
defmain():print("Running build command...")withopen("build.txt", "w")as f: f.write("BUILD_COMMAND")if__name__=="__main__":main()A Build Command defined in vercel.json or in the Project Settings dashboard takes precedence over a build script in pyproject.toml.
Use vercel dev to run your application locally:
python -mvenv.venvsource.venv/bin/activatepip install-rrequirements.txtvercel devDeploy the project by connecting your Git repository or by using the Vercel CLI:
vc deployWhen your Django project has STATIC_ROOT configured, Vercel automatically runs collectstatic during the build and serves the collected files from the Vercel CDN. Files are served at STATIC_URL (Django's default is /static/).
No additional configuration is needed. {% static %} template tags work in both production and local development with vercel dev.
Supported storage backends:
StaticFilesStorage(default)ManifestStaticFilesStorage- WhiteNoise
CompressedManifestStaticFilesStorage
If Vercel detects django-storages as the storage backend, it runs collectstatic with your original settings so files are uploaded directly to your storage provider during the build. Set any required environment variables for your storage provider in your Vercel project environment variables.
WhiteNoise is compatible with Vercel. In production, static files are served from the CDN. WhiteNoise is only active when running locally with vercel dev.
When you add a database or other integration to your Vercel project, Vercel automatically sets environment variables like DATABASE_URL. You can access these in your Django settings through os.environ. See environment variables for more details.
For example, to configure a PostgreSQL database using DATABASE_URL:
import osimport urllib.parseif os.environ.get("DATABASE_URL"): url = urllib.parse.urlparse(os.environ["DATABASE_URL"]) DATABASES ={"default":{"ENGINE":"django.db.backends.postgresql","NAME": url.path.lstrip("/"),"USER": url.username,"PASSWORD": url.password,"HOST": url.hostname,"PORT": url.port,}}else:# Fall back to SQLite for local development DATABASES ={"default":{"ENGINE":"django.db.backends.sqlite3","NAME": BASE_DIR /"db.sqlite3",}}To use environment variables locally (for example, to run migrations), first pull them with vercel pull:
vercel pullThis saves your environment variables to .env.local. Then load them in manage.py using dotenv or django-environ:
from dotenv import load_dotenvload_dotenv(".env.local")import environenviron.Env.read_env(".env.local")When you deploy a Django app to Vercel, it becomes a single Vercel Function. Vercel uses Fluid compute by default, so the function scales with traffic.
To configure that function, add an entry to the functions
object in vercel.json keyed by your
resolved entrypoint file. For example, to let a WSGI app defined in
myproject/wsgi.py run for up to 60 seconds, set maxDuration:
{"$schema":"https://openapi.vercel.sh/vercel.json","functions": {"myproject/wsgi.py": {"maxDuration":60 } }}For an ASGI app, key the entry on myproject/asgi.py instead. If your
manage.py lives in a subdirectory, prefix the key with that directory (for
example backend/myproject/wsgi.py). For more options, see Configuring
functions and the functions
property.
Use Django Channels to add WebSocket consumers and routing to your Django ASGI application. Vercel serves the WebSocket connection from the same Vercel Function as your Django application.
Follow the steps below to get started with WebSockets and Django.
Add
Djangoandchannelsto yourpyproject.toml:pyproject.toml[project]name ="my-django-app"version ="0.1.0"requires-python =">=3.12"dependencies = ["channels>=4.2","Django>=5.1",]Install the dependencies from
pyproject.tomlwithuv:terminaluv sync- myproject/consumers.py
from channels.generic.websocket import AsyncWebsocketConsumerclassEchoConsumer(AsyncWebsocketConsumer):asyncdefconnect(self):await self.accept()asyncdefreceive(self,text_data=None,bytes_data=None):if text_data isnotNone:await self.send(text_data=text_data)elif bytes_data isnotNone:await self.send(bytes_data=bytes_data) Route WebSocket connections to the consumer from your ASGI entrypoint:
myproject/asgi.pyimport osos.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")from django.core.asgi import get_asgi_application# Initialize Django before importing code that may use the app registry.django_asgi_application =get_asgi_application()from channels.routing import ProtocolTypeRouter, URLRouterfrom django.urls import pathfrom myproject.consumers import EchoConsumerapplication =ProtocolTypeRouter( {"http": django_asgi_application,"websocket": URLRouter( [path("api/ws", EchoConsumer.as_asgi()), ] ), })Set
ASGI_APPLICATIONin your Django settings so Vercel loads the ASGI entrypoint:myproject/settings.pyASGI_APPLICATION ="myproject.asgi.application"
For group broadcasts across Vercel Function instances, configure an external
channel layer. InMemoryChannelLayer only coordinates connections within one
function instance. Learn more about managing persistent state, handling
reconnects, and WebSocket limits.
All Vercel Functions limitations apply to Django applications, including:
- Application size: The Django application becomes a single bundle, which has a standard bundle size limit of 500MB. Large Functions support Python bundles up to 5GB on Fluid compute when enabled (public beta).
For more about deploying Django on Vercel, see:
Was this helpful?