Skip to content

Commit 1a2e99f

Browse files
Update django todo example to use todo backend (#97)
* Update django todo d1 example to use todo backend * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9524e36 commit 1a2e99f

15 files changed

Lines changed: 109 additions & 936 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Need to deploy your Worker to Cloudflare? Python Workers are in open beta and ha
3333
- [**`sync-http-clients/`**](sync-http-clients) — demonstrates outbound HTTP with synchronous Python clients (`requests`, `urllib3`, and `httpx.Client`).
3434
- [**`dynamic-py-py/`**](dynamic-py-py) — shows how to load and run a Python Worker dynamically at runtime using a [Worker Loader](https://developers.cloudflare.com/workers/runtime-apis/bindings/worker-loader/) binding.
3535
- [**`django/`**](django) — runs a naive Django WSGI application directly on Python Workers.
36-
- [**`django-todo-d1/`**](django-todo-d1) — uses Django with D1 for a basic TODO application.
36+
- [**`django-todo-d1/`**](django-todo-d1) — implements the Todo-Backend API with Django and D1.
3737
- [**`image-redraw/`**](image-redraw) — an example that combines [FastAPI](https://fastapi.tiangolo.com/), [R2](https://developers.cloudflare.com/r2/), [Queues](https://developers.cloudflare.com/queues/), [Workflows](https://developers.cloudflare.com/workflows/) and [Workers AI](https://developers.cloudflare.com/workers-ai/) to redraw uploaded images.
3838

3939

‎django-todo-d1/README.md‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Django TODO + D1 Example
1+
# Django Todo Backend + D1 Example
22

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

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/).
5+
This example uses Django with [django-cf](https://pypi.org/project/django-cf/) and a D1 database to implement the [Todo-Backend](https://todobackend.com) API. The Worker only serves the backend; use the existing Todo-Backend client as the frontend.
66

77
## How to Run
88

@@ -21,15 +21,26 @@ Start the development server:
2121
uv run pywrangler dev
2222
```
2323

24-
Then open http://localhost:8787/ in your browser.
24+
Then open the Todo-Backend client with the local API URL:
25+
26+
```
27+
https://todobackend.com/client/index.html?http://localhost:8787/todos
28+
```
29+
30+
You can also run the Todo-Backend specification against the Django implementation:
31+
32+
```
33+
https://todobackend.com/specs/index.html?http://localhost:8787/todos
34+
```
2535

2636
## How to deploy
2737

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

30-
Then run
40+
Apply the migrations to the remote D1 database, then deploy the Worker:
3141

3242
```sh
43+
uv run pywrangler d1 migrations apply django-todo-d1 --remote
3344
uv run pywrangler deploy
3445
```
3546

@@ -38,13 +49,14 @@ uv run pywrangler deploy
3849

3950
| Endpoint | Description |
4051
|---|---|
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 |
52+
| `GET /todos` | List all todos |
53+
| `POST /todos` | Create a todo |
54+
| `DELETE /todos` | Delete all todos |
55+
| `GET /todos/<id>` | Fetch one todo |
56+
| `PATCH /todos/<id>` | Partially update a todo |
57+
| `DELETE /todos/<id>` | Delete a todo |
4758

4859
## Notes
4960

5061
- Schema changes are managed by hand-written Wrangler D1 SQL migrations, not Django `manage.py migrate`.
62+
- Cross-origin requests are enabled so the hosted Todo-Backend client can call the Worker.

‎django-todo-d1/migrations/0001_create_todos.sql‎

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE todos (
2+
id TEXT PRIMARY KEY,
3+
title TEXT NOT NULL DEFAULT '',
4+
completed INTEGER NOT NULL DEFAULT 0,
5+
"order" INTEGER
6+
);

‎django-todo-d1/public/app.js‎

Lines changed: 0 additions & 216 deletions
This file was deleted.

0 commit comments

Comments
 (0)