Skip to content
For agents
Browse guides

Django

Run Django or Django REST Framework with explicit production settings and static-file handling.

Open Markdown

Deploy a Django or Django REST Framework project with Gunicorn.

Prepare your settings

This guide assumes manage.py is at the source root and your WSGI module is config/wsgi.py. If yours has another name, replace config.wsgi below. List Django, Gunicorn, your PostgreSQL driver and WhiteNoise (if you use it) in requirements.txt with pinned versions.

In your production settings:

  • Set DEBUG=False.
  • Read SECRET_KEY from an environment variable, and keep the same value across deployments.
  • Set ALLOWED_HOSTS to your app's hostname and CSRF_TRUSTED_ORIGINS to its full https:// address. Use the real values; a wildcard here switches off the protection.

Include static files

To serve the admin's CSS and your own static files from the app, add WhiteNoise to MIDDLEWARE straight after Django's SecurityMiddleware, and set STATIC_ROOT.

collectstatic has to run while the image is built, when there is no database and no secrets. If your settings can load like that, add RUN python manage.py collectstatic --noinput to the Dockerfile after the COPY . . line. If they can't, make a small settings module just for the build. The Dockerfile below leaves this step out.

Run migrations

Smolcloud doesn't run migrate for you. Run it as a step of your own, before you deploy code that needs the new tables. During a deployment the old version keeps serving until the new one is healthy, so each migration must work with both. A code rollback doesn't undo migrations.

Health and server

Add a /health view that returns 200 without touching the database, and set it as the app's health path in Configuration. If the health check is refused, check that ALLOWED_HOSTS and any HTTPS redirect setting accept it.

Use Gunicorn, as below. manage.py runserver is for development only. This setup handles ordinary HTTP; Django Channels and WebSockets need an ASGI server, which this guide doesn't cover.

Build with a Dockerfile

This Dockerfile installs from requirements.txt with pip. If you use another Python version or tool, such as Poetry or uv, change the base image and the install line to match.

Code
FROM python:3.14-slim-bookworm
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PORT=3000
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY --chown=10001:10001 . .
USER 10001:10001
EXPOSE 3000
CMD ["sh", "-c", "exec gunicorn --bind 0.0.0.0:${PORT:-3000} --workers 1 --access-logfile - config.wsgi:application"]

Deploy the app

Save the Dockerfile at the source root. Add a .dockerignore that excludes .env*, .git, .venv and __pycache__, then deploy and check the health path:

Shell
smolcloud deploy APP_ID .
smolcloud request APP_ID /health

Then open the app and try a real page too. Read database settings and other secrets from environment variables, and keep data in PostgreSQL or a bucket: files written inside the container are lost on the next deploy.

See also: Django deployment guidance.