Browse all topics

Frameworks

Python (Django, Flask, FastAPI)

What to set before your first Python deploy, including secrets, hosts and CSRF.

Quick settings#

FrameworkBuild commandRun command
Djangopip install -r requirements.txt && python manage.py collectstatic --noinputgunicorn myproject.wsgi:application --bind 0.0.0.0:$APP_PORT
Flaskpip install -r requirements.txtgunicorn app:app --bind 0.0.0.0:$APP_PORT
FastAPIpip install -r requirements.txtuvicorn main:app --host 0.0.0.0 --port $APP_PORT

Python projects run on Python 3.12 by default; set PYTHON_VERSION (for example 3.11) to use another. Put gunicorn or uvicorn in your requirements.txt, and pin your versions so builds are repeatable.

Django: set these before the first deploy#

Django refuses to start, or answers 400 Bad Request or CSRF verification failed, if its host and secret settings are wrong. Dadisiweb fills in safe defaults as environment variables, but your settings.py must read them:

import os

# The build step does not receive your environment variables, so give the key a fallback.
# The real key is always set when your app runs.
SECRET_KEY = os.environ.get("SECRET_KEY", "dev-only-insecure-key")
DEBUG = os.environ.get("DEBUG", "False") == "True"
ALLOWED_HOSTS = [h for h in os.environ.get("ALLOWED_HOSTS", "").split(",") if h]
CSRF_TRUSTED_ORIGINS = [o for o in os.environ.get("CSRF_TRUSTED_ORIGINS", "").split(",") if o]

STATIC_URL = os.environ.get("STATIC_URL", "/static/")
STATIC_ROOT = os.environ.get("STATIC_ROOT", "/app/staticfiles")
MEDIA_URL = os.environ.get("MEDIA_URL", "/media/")
MEDIA_ROOT = os.environ.get("MEDIA_ROOT", "/data/media")

# We terminate HTTPS for you; Django must trust that.
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

What Dadisiweb sets if you did not:

VariableDefault
SECRET_KEYA strong random key, the same on every redeploy so sessions stay valid.
ALLOWED_HOSTSYour project address and localhost, plus every connected custom domain (and its www).
CSRF_TRUSTED_ORIGINShttps:// plus your project address, plus https:// for each connected custom domain.
DEBUGFalse
STATIC_ROOT/app/staticfiles
MEDIA_ROOT/data/media (persistent)

You do not need to set any of these yourself. Your own values always win; if you set ALLOWED_HOSTS yourself, include every domain you use. To refer to your address without knowing it in advance, use a placeholder such as CSRF_TRUSTED_ORIGINS=${APP_URL}.

Connected a custom domain? Your project is redeployed so the new domain is added to ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS. If you manage those variables yourself, add the domain by hand.

Static files#

gunicorn does not serve static files. Use WhiteNoise:

pip install whitenoise
MIDDLEWARE.insert(1, "whitenoise.middleware.WhiteNoiseMiddleware")
STORAGES = {"staticfiles": {"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage"}}

and keep collectstatic in your build command. Without it, the Django admin appears without styling.

Database#

Create a Dadisiweb database and copy its connection details into environment variables (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT). Do not use SQLite on the app's local disk unless the file lives in /data.

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", "5432"),
}}

Run migrations when the app starts, not during the build (the build has no access to your environment variables or database). Put them in front of your run command:

python manage.py migrate --noinput && gunicorn myproject.wsgi:application --bind 0.0.0.0:$APP_PORT

You can also run python manage.py migrate by hand from the terminal.

Uploads#

Files users upload (MEDIA_ROOT) must live in /data/media. Anything else is deleted on the next deploy. Django does not serve media files in production by itself: put uploads on a storage service, or serve them through a view you control.