|
| 1 | +# Django D1 Template for Cloudflare Workers |
| 2 | + |
| 3 | +This template provides a starting point for running a Django application on Cloudflare Workers, utilizing Cloudflare D1 for serverless SQL database. |
| 4 | + |
| 5 | +[](https://deploy.workers.cloudflare.com/?url=https://github.com/G4brym/django-cf/tree/main/templates/d1) |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +This template is pre-configured to: |
| 10 | +- Use `django-cf` to bridge Django with Cloudflare's environment. |
| 11 | +- Employ Cloudflare D1 as the primary data store through the `django_cf.db.backends.d1` database engine. |
| 12 | +- Include a basic Django project structure within the `src/` directory. |
| 13 | +- Provide example worker entrypoint (`src/index.py`). |
| 14 | + |
| 15 | +## Project Structure |
| 16 | + |
| 17 | +``` |
| 18 | +template-root/ |
| 19 | + |-> src/ |
| 20 | + | |-> manage.py # Django management script |
| 21 | + | |-> index.py # Cloudflare Worker entrypoint |
| 22 | + | |-> app/ # Your Django project (rename as needed) |
| 23 | + | | |-> settings.py # Django settings, configured for D1 |
| 24 | + | | |-> urls.py # Django URLs, includes management endpoints |
| 25 | + | | |-> wsgi.py # WSGI application |
| 26 | + | |-> your_django_apps/ # Add your Django apps here |
| 27 | + | |-> vendor/ # Project dependencies (managed by vendor.txt) |
| 28 | + |-> staticfiles/ # Collected static files (after build) |
| 29 | + |-> .gitignore |
| 30 | + |-> package.json # For Node.js dependencies like wrangler |
| 31 | + |-> uv.lock # Python dependencies lock file |
| 32 | + |-> pyproject.toml # Python project configuration |
| 33 | + |-> wrangler.jsonc # Wrangler configuration |
| 34 | +``` |
| 35 | + |
| 36 | +## Setup and Deployment |
| 37 | + |
| 38 | +1. **Install Dependencies:** |
| 39 | + Ensure you have Node.js, npm, and Python installed. Then: |
| 40 | + |
| 41 | + ```bash |
| 42 | + # Install Node.js dependencies |
| 43 | + npm install |
| 44 | + |
| 45 | + # Install Python dependencies |
| 46 | + uv sync |
| 47 | + ``` |
| 48 | + |
| 49 | + If you don't have `uv` installed, install it first: |
| 50 | + ```bash |
| 51 | + pip install uv |
| 52 | + ``` |
| 53 | +
|
| 54 | +2. **Configure `wrangler.jsonc`:** |
| 55 | + Review and update `wrangler.jsonc` for your project. Key sections: |
| 56 | + * `name`: Your worker's name. |
| 57 | + * `compatibility_date`: Keep this up-to-date. |
| 58 | + * `d1_databases`: |
| 59 | + * `binding`: The name used to access the D1 database in your worker (e.g., "DB"). |
| 60 | + * `database_name`: The name of your D1 database in the Cloudflare dashboard. |
| 61 | + * `database_id`: The ID of your D1 database. |
| 62 | + |
| 63 | + Example `d1_databases` configuration in `wrangler.jsonc`: |
| 64 | + ```jsonc |
| 65 | + { |
| 66 | + "d1_databases": [ |
| 67 | + { |
| 68 | + "binding": "DB", |
| 69 | + "database_name": "my-django-db", |
| 70 | + "database_id": "your-d1-database-id-here" |
| 71 | + } |
| 72 | + ] |
| 73 | + } |
| 74 | + ``` |
| 75 | + |
| 76 | +3. **Django Settings (`src/app/settings.py`):** |
| 77 | + The template should be configured to use D1 binding: |
| 78 | + ```python |
| 79 | + # src/app/settings.py |
| 80 | + DATABASES = { |
| 81 | + 'default': { |
| 82 | + 'ENGINE': 'django_cf.db.backends.d1', |
| 83 | + # This name 'DB' must match the 'binding' in your wrangler.jsonc d1_databases section |
| 84 | + 'CLOUDFLARE_BINDING': 'DB', |
| 85 | + } |
| 86 | + } |
| 87 | + ``` |
| 88 | + |
| 89 | +4. **Worker Entrypoint (`src/index.py`):** |
| 90 | + This file contains the main worker handler for your Django application. |
| 91 | + ```python |
| 92 | + from workers import WorkerEntrypoint |
| 93 | + from django_cf import DjangoCF |
| 94 | +
|
| 95 | + class Default(DjangoCF, WorkerEntrypoint): |
| 96 | + async def get_app(self): |
| 97 | + from app.wsgi import application |
| 98 | + return application |
| 99 | + ``` |
| 100 | + |
| 101 | +5. **Run Development Server:** |
| 102 | + ```bash |
| 103 | + npm run dev |
| 104 | + ``` |
| 105 | + This starts the local development server using Wrangler. |
| 106 | + |
| 107 | +6. **Deploy to Cloudflare:** |
| 108 | + ```bash |
| 109 | + npm run deploy |
| 110 | + ``` |
| 111 | + This command installs system dependencies and deploys your worker to Cloudflare. |
| 112 | + |
| 113 | +## Running Management Commands |
| 114 | + |
| 115 | +For D1, you can use the special management endpoints provided in the template: |
| 116 | + |
| 117 | +* **`/__run_migrations__/`**: Triggers the `migrate` command. |
| 118 | +* **`/__create_admin__/`**: Creates a superuser (username: 'admin', password: 'password'). |
| 119 | + |
| 120 | +These endpoints are defined in `src/app/urls.py` and are protected by `user_passes_test(is_superuser)`. This means you must first create an admin user and be logged in as that user to access these endpoints. |
| 121 | + |
| 122 | +**Initial Admin User Creation:** |
| 123 | +For the very first admin user creation, you might need to temporarily remove the `@user_passes_test(is_superuser)` decorator from `create_admin_view` in `src/app/urls.py`, deploy, access `/__create_admin__/`, and then reinstate the decorator and redeploy. Alternatively, modify the `create_admin_view` to accept a secure token or other mechanism for the initial setup if direct unauthenticated access is undesirable. |
| 124 | + |
| 125 | +**Accessing the Endpoints:** |
| 126 | +Once deployed and an admin user exists (and you are logged in as them): |
| 127 | +- Visit `https://your-worker-url.com/__run_migrations__/` to apply migrations. |
| 128 | +- Visit `https://your-worker-url.com/__create_admin__/` to create the admin user if needed. |
| 129 | + |
| 130 | +Check the JSON response in your browser to see the status of the command. |
| 131 | + |
| 132 | +## Development Notes |
| 133 | + |
| 134 | +* **D1 Limitations:** |
| 135 | + * **Transactions are disabled** for D1. Every query is committed immediately. This is a fundamental aspect of D1. |
| 136 | + * The D1 backend has some limitations compared to traditional SQLite or other SQL databases. Many advanced ORM features or direct SQL functions (especially those used in Django Admin) might not be fully supported. Refer to the `django-cf` README and official Cloudflare D1 documentation. |
| 137 | + * Django Admin functionality might be limited. |
| 138 | +* **Local Testing with D1:** |
| 139 | + * Wrangler allows local development and can simulate D1 access. `npx wrangler dev --remote` can connect to your actual D1 database for more accurate testing. |
| 140 | +* **Security:** |
| 141 | + * The management command endpoints are protected by Django's `user_passes_test(is_superuser)`. Ensure they are properly secured before deploying to production. |
| 142 | + * Protect your Cloudflare credentials and API tokens. |
| 143 | +
|
| 144 | +--- |
| 145 | +*For more details on `django-cf` features and configurations, refer to the main [django-cf GitHub repository](https://github.com/G4brym/django-cf).* |
0 commit comments