Simple Django application that captures qualitative survey responses for the getsva research initiative.
cd /Users/anuragsingh/Documents/GitHub/survey
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python manage.py migrate
python manage.py runserverVisit http://127.0.0.1:8000 to fill out the form.
source .venv/bin/activate
python manage.py testCloudflare’s Python Workers runtime can host this Django app as long as the database lives in a remotely reachable service (Neon, Supabase, RDS, etc.). The checklist below covers everything you need to do locally before the deploy, followed by the exact steps to run inside Cloudflare.
-
Create env files
cd /Users/anuragsingh/Documents/GitHub/survey cp env.example env.local cp env.example env.productionFill in:
ENVIRONMENT=local(orproductioninsideenv.production)SECRET_KEY=new random stringDEBUG=falsein prodALLOWED_HOSTS=survey.getsva.com,*.workers.devCSRF_TRUSTED_ORIGINS=https://survey.getsva.com,https://<your-worker>.workers.devDATABASE_URL=connection string for your managed cloud DB (e.g.,postgres://user:pass@host:5432/dbname)- Any email/S3 keys you need later.
-
Update Django settings
- In
survey_site/settings.py, load env vars (e.g., usingos.environordjango-environ) and configure:SECRET_KEY = os.environ["SECRET_KEY"]DEBUG = os.environ.get("DEBUG", "false").lower() == "true"ALLOWED_HOSTS = os.environ["ALLOWED_HOSTS"].split(",")CSRF_TRUSTED_ORIGINS = os.environ["CSRF_TRUSTED_ORIGINS"].split(",")DATABASES["default"]fromDATABASE_URLwhen present (fall back to SQLite for local dev).
- Set
STATIC_ROOT = BASE_DIR / "staticfiles"socollectstatichas a deterministic output.
- In
-
Expose Django as ASGI
- Ensure
survey_site/__init__.pysetsDJANGO_SETTINGS_MODULE. - Add
worker.pyat the repo root:import os import django from asgiref.wsgi import WsgiToAsgi from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "survey_site.settings") django.setup() asgi_app = WsgiToAsgi(get_wsgi_application())
- Ensure
-
Create the Worker project
npm install -g wrangler wrangler init survey-worker --python --no-config mv worker.py functions
Structure after this step (key files only):
. ├─ functions/ │ └─ _worker.py ├─ survey_site/ ├─ surveys/ ├─ worker.py ├─ wrangler.toml -
Wire the Worker entry point
- Replace
functions/_worker.pywith:from worker import asgi_app async def on_fetch(request, env, ctx): return await asgi_app(request, env, ctx)
- Set up
wrangler.toml:name = "survey-getSVA" main = "functions/_worker.py" compatibility_date = "2025-11-17" compatibility_flags = ["python_workers"] [vars] DJANGO_SETTINGS_MODULE = "survey_site.settings" SECRET_KEY = "copy-from-.env.production" ALLOWED_HOSTS = "survey.getsva.com,*.workers.dev" CSRF_TRUSTED_ORIGINS = "https://survey.getsva.com,https://survey-getSVA.workers.dev" DATABASE_URL = "postgres://..."
- Replace
-
Install dependencies & freeze
python -m pip install -r requirements.txt pip freeze > requirements.txt # ensures Worker build sees exact versions
-
Static assets
python manage.py collectstatic --settings=survey_site.settings --noinput
Upload the resulting
staticfiles/directory to Cloudflare R2 (recommended) or keep it alongside the Worker and serve via WhiteNoise if the footprint is small. -
Local validation
python manage.py migrate --settings=survey_site.settings python manage.py testRun against your managed DB (temporarily allow your IP if needed) to confirm migrations succeed before the Worker touches production data.
Commit all necessary files (worker.py, functions/_worker.py, wrangler.toml, updated settings) and push so Wrangler can deploy from the clean state.
-
Authenticate Wrangler
cd /Users/anuragsingh/Documents/GitHub/survey wrangler login -
Deploy the Worker
wrangler deploy wrangler tail # stream logs for sanity checkEnsure your external database allows connections from Cloudflare’s egress IPs (most managed Postgres/Supabase instances permit this by default).
-
Run migrations in the Worker environment
wrangler invoke survey-getSVA --command "python manage.py migrate --settings=survey_site.settings" -
Bind secrets (optional) For values you do not want inside
wrangler.toml, use:wrangler secret put SECRET_KEY wrangler secret put DATABASE_URL
and remove them from the
[vars]block. -
Attach the custom domain
- Cloudflare Dashboard → Workers & Pages →
survey-getSVA→ Triggers → Add Custom Domain →survey.getsva.com. - Accept the suggested CNAME DNS record; keep the proxy enabled (orange cloud).
- Enable “Always use HTTPS” under SSL/TLS → Edge Certificates.
- Cloudflare Dashboard → Workers & Pages →
-
Cache controls
- Create a Cache Rule:
if path starts_with "/admin"or Request Method isPOST→ Cache level: Bypass. - Optional rule to cache static assets aggressively if served via Worker/R2.
- Create a Cache Rule:
-
Smoke test
- Visit the workers.dev preview URL to ensure forms work and data lands in the DB.
- After DNS propagates, verify
https://survey.getsva.comfor both survey form and/admin. - Monitor logs via
wrangler tailduring initial traffic.
Once these steps are complete, the Django app runs entirely inside Cloudflare’s Python Workers runtime while persisting data to your managed cloud database.