Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 18-django/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Django WSGI Example

[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/python-workers-examples/tree/main/18-django)

This example runs a small, conventional Django project directly on Cloudflare Workers using Django's WSGI application.

## How to Run

First ensure that `uv` is installed:
https://docs.astral.sh/uv/getting-started/installation/#standalone-installer

Run:

```sh
uv run pywrangler dev
```

Then try:

```sh
curl http://localhost:8787/
```

You should receive:

```json
{"message": "Hello from Django on Cloudflare Workers!"}
```

You can also deploy with:

```sh
uv run pywrangler deploy
```
13 changes: 13 additions & 0 deletions 18-django/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "django-worker",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "uv run pywrangler deploy",
"dev": "uv run pywrangler dev",
"start": "uv run pywrangler dev"
},
"devDependencies": {
"wrangler": "^4.114.0"
}
}
15 changes: 15 additions & 0 deletions 18-django/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[project]
name = "django-worker"
version = "0.1.0"
description = "Simple Django example for Python Workers"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"django",
]

[dependency-groups]
dev = [
"workers-py",
"workers-runtime-sdk",
]
12 changes: 12 additions & 0 deletions 18-django/src/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os

from workers import WorkerEntrypoint, wsgi

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_django.settings")

from hello_django.wsgi import application


class Default(WorkerEntrypoint):
async def fetch(self, request):
return await wsgi.fetch(application, request, self.env)
Empty file.
8 changes: 8 additions & 0 deletions 18-django/src/hello_django/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SECRET_KEY = "django-insecure-development-placeholder"
DEBUG = False
ALLOWED_HOSTS = ["*"]
ROOT_URLCONF = "hello_django.urls"
MIDDLEWARE = []
INSTALLED_APPS = []
WSGI_APPLICATION = "hello_django.wsgi.application"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
11 changes: 11 additions & 0 deletions 18-django/src/hello_django/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.http import JsonResponse
from django.urls import path


def root(_request):
return JsonResponse({"message": "Hello from Django on Cloudflare Workers!"})


urlpatterns = [
path("", root),
]
7 changes: 7 additions & 0 deletions 18-django/src/hello_django/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_django.settings")

application = get_wsgi_application()
12 changes: 12 additions & 0 deletions 18-django/wrangler.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "django-worker",
"main": "src/entry.py",
"compatibility_date": "2026-07-29",
"compatibility_flags": [
"python_workers"
],
"observability": {
"enabled": true
}
}
50 changes: 50 additions & 0 deletions 19-django-todo-d1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Django TODO + D1 Example

[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/python-workers-examples/tree/main/19-django-todo-d1)

This example uses Django with [django-cf](https://pypi.org/project/django-cf/) and a D1 database to serve a small TODO API under `/api/*`, plus a static frontend from `public/` served by [Workers Static Assets](https://developers.cloudflare.com/workers/static-assets/).

## How to Run

First ensure that `uv` is installed:
https://docs.astral.sh/uv/getting-started/installation/#standalone-installer

Apply local migrations before development:

```sh
uv run pywrangler d1 migrations apply django-todo-d1 --local
```

Start the development server:

```sh
uv run pywrangler dev
```

Then open http://localhost:8787/ in your browser.

## How to deploy

Replace the `database_id` in `wrangler.jsonc` with your actual D1 database UUID.

Then run

```sh
uv run pywrangler deploy
```


## Endpoints

| Endpoint | Description |
|---|---|
| `GET /api/health/` | Health check |
| `GET /api/todos/` | List TODOs, newest first |
| `POST /api/todos/` | Create a TODO |
| `GET /api/todos/<id>/` | Fetch one TODO |
| `PATCH /api/todos/<id>/` | Partially update `title` and/or `completed` |
| `DELETE /api/todos/<id>/` | Delete a TODO |

## Notes

- Schema changes are managed by hand-written Wrangler D1 SQL migrations, not Django `manage.py migrate`.
9 changes: 9 additions & 0 deletions 19-django-todo-d1/migrations/0001_create_todos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(200) NOT NULL,
completed BOOLEAN NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS todos_created_at_id_idx
ON todos (created_at DESC, id DESC);
13 changes: 13 additions & 0 deletions 19-django-todo-d1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "django-todo-d1-worker",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "uv run pywrangler deploy",
"dev": "uv run pywrangler dev",
"start": "uv run pywrangler dev"
},
"devDependencies": {
"wrangler": "^4.114.0"
}
}
Loading
Loading