Skip to content

Commit affcbff

Browse files
ryanking13dom96
andauthored
Add examples using django (#92)
* Add examples using django * Update 18-django/pyproject.toml Co-authored-by: Dominik Picheta <dominik@cloudflare.com> * chore: update readme --------- Co-authored-by: Dominik Picheta <dominik@cloudflare.com>
1 parent f2a35b9 commit affcbff

28 files changed

Lines changed: 1278 additions & 0 deletions

18-django/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Django WSGI Example
2+
3+
[![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)
4+
5+
This example runs a small, conventional Django project directly on Cloudflare Workers using Django's WSGI application.
6+
7+
## How to Run
8+
9+
First ensure that `uv` is installed:
10+
https://docs.astral.sh/uv/getting-started/installation/#standalone-installer
11+
12+
Run:
13+
14+
```sh
15+
uv run pywrangler dev
16+
```
17+
18+
Then try:
19+
20+
```sh
21+
curl http://localhost:8787/
22+
```
23+
24+
You should receive:
25+
26+
```json
27+
{"message": "Hello from Django on Cloudflare Workers!"}
28+
```
29+
30+
You can also deploy with:
31+
32+
```sh
33+
uv run pywrangler deploy
34+
```

18-django/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "django-worker",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "uv run pywrangler deploy",
7+
"dev": "uv run pywrangler dev",
8+
"start": "uv run pywrangler dev"
9+
},
10+
"devDependencies": {
11+
"wrangler": "^4.114.0"
12+
}
13+
}

18-django/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[project]
2+
name = "django-worker"
3+
version = "0.1.0"
4+
description = "Simple Django example for Python Workers"
5+
readme = "README.md"
6+
requires-python = ">=3.13"
7+
dependencies = [
8+
"django",
9+
]
10+
11+
[dependency-groups]
12+
dev = [
13+
"workers-py",
14+
"workers-runtime-sdk",
15+
]

18-django/src/entry.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
3+
from workers import WorkerEntrypoint, wsgi
4+
5+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_django.settings")
6+
7+
from hello_django.wsgi import application
8+
9+
10+
class Default(WorkerEntrypoint):
11+
async def fetch(self, request):
12+
return await wsgi.fetch(application, request, self.env)

18-django/src/hello_django/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SECRET_KEY = "django-insecure-development-placeholder"
2+
DEBUG = False
3+
ALLOWED_HOSTS = ["*"]
4+
ROOT_URLCONF = "hello_django.urls"
5+
MIDDLEWARE = []
6+
INSTALLED_APPS = []
7+
WSGI_APPLICATION = "hello_django.wsgi.application"
8+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

18-django/src/hello_django/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.http import JsonResponse
2+
from django.urls import path
3+
4+
5+
def root(_request):
6+
return JsonResponse({"message": "Hello from Django on Cloudflare Workers!"})
7+
8+
9+
urlpatterns = [
10+
path("", root),
11+
]

18-django/src/hello_django/wsgi.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
from django.core.wsgi import get_wsgi_application
4+
5+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_django.settings")
6+
7+
application = get_wsgi_application()

18-django/wrangler.jsonc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "node_modules/wrangler/config-schema.json",
3+
"name": "django-worker",
4+
"main": "src/entry.py",
5+
"compatibility_date": "2026-07-29",
6+
"compatibility_flags": [
7+
"python_workers"
8+
],
9+
"observability": {
10+
"enabled": true
11+
}
12+
}

19-django-todo-d1/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Django TODO + D1 Example
2+
3+
[![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)
4+
5+
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/).
6+
7+
## How to Run
8+
9+
First ensure that `uv` is installed:
10+
https://docs.astral.sh/uv/getting-started/installation/#standalone-installer
11+
12+
Apply local migrations before development:
13+
14+
```sh
15+
uv run pywrangler d1 migrations apply django-todo-d1 --local
16+
```
17+
18+
Start the development server:
19+
20+
```sh
21+
uv run pywrangler dev
22+
```
23+
24+
Then open http://localhost:8787/ in your browser.
25+
26+
## How to deploy
27+
28+
Replace the `database_id` in `wrangler.jsonc` with your actual D1 database UUID.
29+
30+
Then run
31+
32+
```sh
33+
uv run pywrangler deploy
34+
```
35+
36+
37+
## Endpoints
38+
39+
| Endpoint | Description |
40+
|---|---|
41+
| `GET /api/health/` | Health check |
42+
| `GET /api/todos/` | List TODOs, newest first |
43+
| `POST /api/todos/` | Create a TODO |
44+
| `GET /api/todos/<id>/` | Fetch one TODO |
45+
| `PATCH /api/todos/<id>/` | Partially update `title` and/or `completed` |
46+
| `DELETE /api/todos/<id>/` | Delete a TODO |
47+
48+
## Notes
49+
50+
- Schema changes are managed by hand-written Wrangler D1 SQL migrations, not Django `manage.py migrate`.

0 commit comments

Comments
 (0)