You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Optional: Settings for local development using D1 API
120
-
# You can create a separate settings_dev.py or use environment variables
121
-
# DATABASES_DEV = {
122
-
# 'default': {
123
-
# 'ENGINE': 'django_cf.d1_api',
124
-
# 'CLOUDFLARE_DATABASE_ID': '<your_database_id>',
125
-
# 'CLOUDFLARE_ACCOUNT_ID': '<your_account_id>',
126
-
# 'CLOUDFLARE_TOKEN': '<your_d1_api_token>',
127
-
# }
128
-
# }
129
87
```
130
88
131
-
4. **Worker Entrypoint (`src/worker.py`):**
132
-
This file contains the main `on_fetch` handler for your Django application.
89
+
4. **Worker Entrypoint (`src/index.py`):**
90
+
This file contains the main worker handler for your Django application.
133
91
```python
134
-
from django_cf import DjangoCFAdapter
92
+
from workers import WorkerEntrypoint
93
+
from django_cf import DjangoCF
135
94
136
-
async def on_fetch(request, env):
137
-
# Ensure your Django project's WSGI application is importable
138
-
# For example, if your project is 'app' inside 'src':
139
-
from app.wsgi import application # Import application inside on_fetch
95
+
class Default(DjangoCF, WorkerEntrypoint):
96
+
async def get_app(self):
97
+
from app.wsgi import application
98
+
return application
99
+
```
140
100
141
-
# The DjangoCFAdapter requires the Django application and the environment (for D1 binding)
142
-
adapter = DjangoCFAdapter(application, env)
143
-
return await adapter.handle_request(request)
101
+
5. **Run Development Server:**
102
+
```bash
103
+
npm run dev
144
104
```
105
+
This starts the local development server using Wrangler.
145
106
146
-
5. **Deploy to Cloudflare:**
107
+
6. **Deploy to Cloudflare:**
147
108
```bash
148
-
npx wrangler deploy
109
+
npm run deploy
149
110
```
150
-
This commandwill also run the `collectstatic`commandif configured in`wrangler.jsonc`.
111
+
This commandinstalls system dependencies and deploys your worker to Cloudflare.
151
112
152
113
## Running Management Commands
153
114
154
-
***Migrations &`createsuperuser` (Local Development - Recommended for D1):**
155
-
For D1, it's often easiest and safest to run migrations and `createsuperuser` from your local machine by configuring your development Django settings to use the D1 API.
156
-
157
-
1. Ensure you have `django-cf` and your other dependencies installed in your local Python environment (`pip install -r requirements-dev.txt`).
158
-
2. Set up your Django settings for local D1 API access. You can do this by:
159
-
* Creating a `settings_dev.py` and using `python src/manage.py migrate --settings=app.settings_dev`.
160
-
* Or, by temporarily modifying your main `settings.py` (ensure you don't commit API keys).
161
-
* Or, by using environment variables to supply D1 API credentials to your settings.
162
-
163
-
Example local D1 API settings in`settings_dev.py` (place it alongside your main `settings.py`):
When you need to generate new migration files based on your model changes, you should run `makemigrations`. However, because the D1 database engine (`django_cf.d1_binding` or `django_cf.d1_api`) requires a live connection to introspect the database schema (which might not be available or desirable during the `makemigrations` phase, especially in CI environments or when you only want to generate files without querying D1), you can use the `WORKERS_CI=1` environment variable. This variable signals `django-cf` to use a dummy database engine for schema-related operations that don't strictly need a live D1 connection, allowing `makemigrations` to run.
After generating migration files, you can apply them using the `migrate` command as shown above (locally with D1 API access) or via a secured worker endpoint if you have one.
194
-
195
-
* **Via a Worker Endpoint (Use with Extreme Caution):**
196
-
While possible, running migrations or creating users directly via a worker endpoint is generally **not recommended for D1** due to the direct database access available locally via the D1 API. If you absolutely must, you can adapt the management command endpoints from the Durable Objects template (`src/app/urls.py`), but ensure they are **extremely well-secured**.
197
-
The main risk is exposing sensitive operations over HTTP and potential complexities with the worker environment.
198
-
199
-
If you choose this path, remember:
200
-
* The worker needs the D1 binding (`env`) passed to the `DjangoCFAdapter` and accessible to your management command functions.
201
-
* Secure the endpoints robustly (e.g., IP restrictions, strong authentication tokens, not just Django's `is_superuser`if the admin user might not exist yet).
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.
202
131
203
132
## Development Notes
204
133
@@ -208,10 +137,9 @@ template-root/
208
137
* Django Admin functionality might be limited.
209
138
***Local Testing with D1:**
210
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.
211
-
* Using the D1 API forlocal management tasks (as described above) is the most common workflow.
212
140
***Security:**
213
-
*If you implement management command endpoints on your worker, secure them rigorously.
214
-
* Protect your Cloudflare API tokens and credentials.
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.
215
143
216
144
---
217
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