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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Need to deploy your Worker to Cloudflare? Python Workers are in open beta and ha
- [**`sync-http-clients/`**](sync-http-clients) — demonstrates outbound HTTP with synchronous Python clients (`requests`, `urllib3`, and `httpx.Client`).
- [**`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.
- [**`django/`**](django) — runs a naive Django WSGI application directly on Python Workers.
- [**`django-todo-d1/`**](django-todo-d1) — uses Django with D1 for a basic TODO application.
- [**`django-todo-d1/`**](django-todo-d1) — implements the Todo-Backend API with Django and D1.
- [**`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.


Expand Down
32 changes: 22 additions & 10 deletions django-todo-d1/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Django TODO + D1 Example
# Django Todo Backend + 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/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/).
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.

## How to Run

Expand All @@ -21,15 +21,26 @@ Start the development server:
uv run pywrangler dev
```

Then open http://localhost:8787/ in your browser.
Then open the Todo-Backend client with the local API URL:

```
https://todobackend.com/client/index.html?http://localhost:8787/todos
```

You can also run the Todo-Backend specification against the Django implementation:

```
https://todobackend.com/specs/index.html?http://localhost:8787/todos
```

## How to deploy

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

Then run
Apply the migrations to the remote D1 database, then deploy the Worker:

```sh
uv run pywrangler d1 migrations apply django-todo-d1 --remote
uv run pywrangler deploy
```

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

| 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 |
| `GET /todos` | List all todos |
| `POST /todos` | Create a todo |
| `DELETE /todos` | Delete all todos |
| `GET /todos/<id>` | Fetch one todo |
| `PATCH /todos/<id>` | Partially update a todo |
| `DELETE /todos/<id>` | Delete a todo |

## Notes

- Schema changes are managed by hand-written Wrangler D1 SQL migrations, not Django `manage.py migrate`.
- Cross-origin requests are enabled so the hosted Todo-Backend client can call the Worker.
9 changes: 0 additions & 9 deletions django-todo-d1/migrations/0001_create_todos.sql

This file was deleted.

6 changes: 6 additions & 0 deletions django-todo-d1/migrations/0001_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE todos (
id TEXT PRIMARY KEY,
title TEXT NOT NULL DEFAULT '',
completed INTEGER NOT NULL DEFAULT 0,
"order" INTEGER
);
216 changes: 0 additions & 216 deletions django-todo-d1/public/app.js

This file was deleted.

Loading
Loading