djact 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- djact-1.0.0/LICENSE +21 -0
- djact-1.0.0/MANIFEST.in +6 -0
- djact-1.0.0/PKG-INFO +384 -0
- djact-1.0.0/README.md +331 -0
- djact-1.0.0/djact/__init__.py +10 -0
- djact-1.0.0/djact/apps.py +10 -0
- djact-1.0.0/djact/middleware.py +46 -0
- djact-1.0.0/djact/py.typed +1 -0
- djact-1.0.0/djact/render.py +99 -0
- djact-1.0.0/djact/static/djact/app.js +591 -0
- djact-1.0.0/djact/templates/djact/djact.html +83 -0
- djact-1.0.0/djact/utils.py +23 -0
- djact-1.0.0/djact.egg-info/PKG-INFO +384 -0
- djact-1.0.0/djact.egg-info/SOURCES.txt +17 -0
- djact-1.0.0/djact.egg-info/dependency_links.txt +1 -0
- djact-1.0.0/djact.egg-info/requires.txt +1 -0
- djact-1.0.0/djact.egg-info/top_level.txt +1 -0
- djact-1.0.0/pyproject.toml +52 -0
- djact-1.0.0/setup.cfg +4 -0
djact-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 djact contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
djact-1.0.0/MANIFEST.in
ADDED
djact-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: djact
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Inertia.js-like bridge between Django and React — server-driven SPA without a REST API.
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2024 djact contributors
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Project-URL: Homepage, https://github.com/yourusername/djact
|
|
28
|
+
Project-URL: Repository, https://github.com/yourusername/djact
|
|
29
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/djact/issues
|
|
30
|
+
Keywords: django,react,inertia,spa,frontend,bridge
|
|
31
|
+
Classifier: Development Status :: 4 - Beta
|
|
32
|
+
Classifier: Environment :: Web Environment
|
|
33
|
+
Classifier: Framework :: Django
|
|
34
|
+
Classifier: Framework :: Django :: 4.0
|
|
35
|
+
Classifier: Framework :: Django :: 4.1
|
|
36
|
+
Classifier: Framework :: Django :: 4.2
|
|
37
|
+
Classifier: Framework :: Django :: 5.0
|
|
38
|
+
Classifier: Framework :: Django :: 5.1
|
|
39
|
+
Classifier: Intended Audience :: Developers
|
|
40
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
41
|
+
Classifier: Operating System :: OS Independent
|
|
42
|
+
Classifier: Programming Language :: Python :: 3
|
|
43
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
46
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
47
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
48
|
+
Requires-Python: >=3.10
|
|
49
|
+
Description-Content-Type: text/markdown
|
|
50
|
+
License-File: LICENSE
|
|
51
|
+
Requires-Dist: django>=4.0
|
|
52
|
+
Dynamic: license-file
|
|
53
|
+
|
|
54
|
+
# djact ⚛️🐍
|
|
55
|
+
|
|
56
|
+
Welcome to **djact**! This is a complete, step-by-step guide to building your first project using Django and React without the headache of APIs.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## 1. What is Djact?
|
|
61
|
+
|
|
62
|
+
Think of **djact** as a bridge.
|
|
63
|
+
|
|
64
|
+
- **Django** handles the "Brain": It manages your database, users, and page logic.
|
|
65
|
+
- **React** handles the "Beauty": It renders the user interface (UI) and makes it feel fast.
|
|
66
|
+
- **No REST API Needed**: You don't need to build complex endpoints. You just pass data from Django to React like you would with a normal template.
|
|
67
|
+
- **SPA Feel**: Your app feels like a Single Page Application (fast and fluid) because Djact only swaps the content of the page when you click a link.
|
|
68
|
+
|
|
69
|
+
## 2. Installation
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install djact
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 3. Create your Django Project
|
|
76
|
+
|
|
77
|
+
Let's start a fresh project from scratch.
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
# Create the project folder
|
|
81
|
+
django-admin startproject myproject
|
|
82
|
+
|
|
83
|
+
# Go inside the project
|
|
84
|
+
cd myproject
|
|
85
|
+
|
|
86
|
+
# Create an app called "core"
|
|
87
|
+
python manage.py startapp core
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**What just happened?**
|
|
91
|
+
You now have a folder named `myproject` containing your project settings and an app named `core` where we will write our code.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## 4. Django Setup
|
|
96
|
+
|
|
97
|
+
Open `myproject/settings.py` and make these changes:
|
|
98
|
+
|
|
99
|
+
### Add to `INSTALLED_APPS`
|
|
100
|
+
Tell Django to use `djact` and your new `core` app.
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
INSTALLED_APPS = [
|
|
104
|
+
...
|
|
105
|
+
"djact",
|
|
106
|
+
"core",
|
|
107
|
+
]
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Add Middleware
|
|
111
|
+
Middleware is code that runs on every request. This allows Djact to detect when a user is navigating.
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
MIDDLEWARE = [
|
|
115
|
+
...
|
|
116
|
+
"djact.middleware.DjactMiddleware",
|
|
117
|
+
]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## 5. Create a View
|
|
123
|
+
|
|
124
|
+
In Django, a "View" is the function that handles a specific URL.
|
|
125
|
+
|
|
126
|
+
Open `core/views.py` and paste this:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from djact import djact_render
|
|
130
|
+
|
|
131
|
+
def home(request):
|
|
132
|
+
return djact_render(request, "Home", {
|
|
133
|
+
"name": "Ankur"
|
|
134
|
+
})
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**Explanation:**
|
|
138
|
+
We are telling Django to render a React component named **"Home"** and pass it a prop named `name` with the value `"Ankur"`.
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 6. Create URLs
|
|
143
|
+
|
|
144
|
+
We need to tell Django which URL should trigger our view.
|
|
145
|
+
|
|
146
|
+
### Step 1: Create `core/urls.py`
|
|
147
|
+
Create a new file at `core/urls.py` and add:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from django.urls import path
|
|
151
|
+
from .views import home
|
|
152
|
+
|
|
153
|
+
urlpatterns = [
|
|
154
|
+
path("", home, name="home"),
|
|
155
|
+
]
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Step 2: Update `myproject/urls.py`
|
|
159
|
+
Update your project's main URL file to include the one we just made:
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
from django.urls import path, include
|
|
163
|
+
|
|
164
|
+
urlpatterns = [
|
|
165
|
+
path("", include("core.urls")),
|
|
166
|
+
]
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## 7. Template Setup
|
|
172
|
+
|
|
173
|
+
Djact needs one basic HTML file to "mount" React.
|
|
174
|
+
|
|
175
|
+
1. Create a folder named `templates` in your project root.
|
|
176
|
+
2. Inside `templates`, create a file named `djact.html`.
|
|
177
|
+
|
|
178
|
+
### File: `templates/djact.html`
|
|
179
|
+
|
|
180
|
+
```html
|
|
181
|
+
<!DOCTYPE html>
|
|
182
|
+
<html lang="en">
|
|
183
|
+
<head>
|
|
184
|
+
<meta charset="UTF-8">
|
|
185
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
186
|
+
<meta name="csrf-token" content="{{ csrf_token }}">
|
|
187
|
+
<title>My Djact App</title>
|
|
188
|
+
</head>
|
|
189
|
+
<body>
|
|
190
|
+
<!-- React will mount inside this div -->
|
|
191
|
+
<div id="app" data-page='{{ page|safe }}'></div>
|
|
192
|
+
|
|
193
|
+
<!-- This is your bundled React JavaScript -->
|
|
194
|
+
<script type="module" src="/static/dist/main.js"></script>
|
|
195
|
+
</body>
|
|
196
|
+
</html>
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
**Why is this needed?**
|
|
200
|
+
This is the "shell". Django loads this file once, and then React takes over the `div#app`. The `data-page` attribute is how Django "sends" the initial props to React.
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## 8. React Setup
|
|
205
|
+
|
|
206
|
+
Now let's set up the frontend using Vite.
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
# In your project root, run:
|
|
210
|
+
npm create vite@latest frontend -- --template react
|
|
211
|
+
|
|
212
|
+
# Go into the frontend folder
|
|
213
|
+
cd frontend
|
|
214
|
+
|
|
215
|
+
# Install dependencies
|
|
216
|
+
npm install
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## 9. The Bridge: `main.jsx`
|
|
222
|
+
|
|
223
|
+
This is the most important file in the frontend. It connects Django to React.
|
|
224
|
+
|
|
225
|
+
Open `frontend/src/main.jsx` and replace everything with:
|
|
226
|
+
|
|
227
|
+
```jsx
|
|
228
|
+
import { createDjactApp } from "djact/static/djact/app.js";
|
|
229
|
+
|
|
230
|
+
createDjactApp({
|
|
231
|
+
resolve: (name) => import(`./Pages/${name}.jsx`),
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**What is happening here?**
|
|
236
|
+
- `createDjactApp`: This is the Djact engine. It reads the `data-page` from the HTML we created in Step 7.
|
|
237
|
+
- `resolve`: This function tells Djact where to find your React files. When Django says "Render Home", Djact looks for `./Pages/Home.jsx`.
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## 10. Create React Pages
|
|
242
|
+
|
|
243
|
+
Create a folder named `Pages` inside `frontend/src/`. Then create your first page.
|
|
244
|
+
|
|
245
|
+
### File: `frontend/src/Pages/Home.jsx`
|
|
246
|
+
|
|
247
|
+
```jsx
|
|
248
|
+
export default function Home({ name }) {
|
|
249
|
+
return (
|
|
250
|
+
<div style={{ padding: '2rem', textAlign: 'center' }}>
|
|
251
|
+
<h1>Hello {name}!</h1>
|
|
252
|
+
<p>Welcome to your Djact application.</p>
|
|
253
|
+
</div>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
**Note:** The `name` prop comes directly from the Django view we wrote in Step 5!
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## 11. Build React
|
|
263
|
+
|
|
264
|
+
Before Django can see your React code, you must build it.
|
|
265
|
+
|
|
266
|
+
In your `frontend` folder, open `vite.config.js` and set the build output to your Django static folder:
|
|
267
|
+
|
|
268
|
+
```javascript
|
|
269
|
+
// vite.config.js
|
|
270
|
+
import { defineConfig } from 'vite'
|
|
271
|
+
import react from '@vitejs/plugin-react'
|
|
272
|
+
|
|
273
|
+
export default defineConfig({
|
|
274
|
+
plugins: [react()],
|
|
275
|
+
build: {
|
|
276
|
+
outDir: '../static/dist', // Build directly into Django's static folder
|
|
277
|
+
rollupOptions: {
|
|
278
|
+
output: {
|
|
279
|
+
entryFileNames: `[name].js`,
|
|
280
|
+
chunkFileNames: `[name].js`,
|
|
281
|
+
assetFileNames: `[name].[ext]`,
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
})
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Now run the build:
|
|
289
|
+
```bash
|
|
290
|
+
npm run build
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
## 12. Run the Project
|
|
296
|
+
|
|
297
|
+
Go back to your project root (where `manage.py` is) and start the server:
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
python manage.py runserver
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
Open your browser and visit: **http://127.0.0.1:8000/**
|
|
304
|
+
|
|
305
|
+
You should see **"Hello Ankur!"**
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## 13. Navigation (Moving between pages)
|
|
310
|
+
|
|
311
|
+
To move between pages without a full reload, use the `<Link>` component.
|
|
312
|
+
|
|
313
|
+
```jsx
|
|
314
|
+
import { Link } from "djact/static/djact/app.js";
|
|
315
|
+
|
|
316
|
+
export default function Home({ name }) {
|
|
317
|
+
return (
|
|
318
|
+
<div>
|
|
319
|
+
<h1>Hello {name}</h1>
|
|
320
|
+
<Link href="/about">Go to About Page</Link>
|
|
321
|
+
</div>
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**How it works:**
|
|
327
|
+
When you click the Link, Djact tells Django "Hey, I need the About page data". Django sends back the props, and React swaps the component instantly.
|
|
328
|
+
|
|
329
|
+
---
|
|
330
|
+
|
|
331
|
+
## 14. Full Project Structure
|
|
332
|
+
|
|
333
|
+
This is what your project should look like:
|
|
334
|
+
|
|
335
|
+
```text
|
|
336
|
+
myproject/
|
|
337
|
+
├── core/
|
|
338
|
+
│ ├── urls.py
|
|
339
|
+
│ └── views.py
|
|
340
|
+
├── myproject/
|
|
341
|
+
│ ├── settings.py
|
|
342
|
+
│ └── urls.py
|
|
343
|
+
├── templates/
|
|
344
|
+
│ └── djact.html
|
|
345
|
+
├── static/
|
|
346
|
+
│ └── dist/ (Generated by npm run build)
|
|
347
|
+
├── frontend/
|
|
348
|
+
│ ├── src/
|
|
349
|
+
│ │ ├── Pages/
|
|
350
|
+
│ │ │ └── Home.jsx
|
|
351
|
+
│ │ └── main.jsx
|
|
352
|
+
│ └── vite.config.js
|
|
353
|
+
└── manage.py
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
---
|
|
357
|
+
|
|
358
|
+
## 15. Common Questions
|
|
359
|
+
|
|
360
|
+
**Q: Where should I write my React code?**
|
|
361
|
+
A: Inside `frontend/src/Pages/`. Each file should match the name you use in `djact_render`.
|
|
362
|
+
|
|
363
|
+
**Q: Do I need React Router?**
|
|
364
|
+
A: **No.** Django handles all the routes in `urls.py`.
|
|
365
|
+
|
|
366
|
+
**Q: Why do I need to run `npm run build`?**
|
|
367
|
+
A: Browsers cannot read `.jsx` files directly. The build step converts them into a single `.js` file that the browser understands.
|
|
368
|
+
|
|
369
|
+
**Q: Can I use multiple Django apps?**
|
|
370
|
+
A: Yes! You can organize your pages however you like. Just make sure your `resolve` function in `main.jsx` can find them.
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
## 16. Common Mistakes to Avoid
|
|
375
|
+
|
|
376
|
+
1. **Forgetting to Build**: If you change your React code, you must run `npm run build` (or run Vite in watch mode) for Django to see the changes.
|
|
377
|
+
2. **Missing Middleware**: If you get a "Page not found" error or the link reloads the whole page, check if `DjactMiddleware` is in `settings.py`.
|
|
378
|
+
3. **Wrong Static Path**: Make sure the `<script>` tag in `djact.html` matches where Vite is building your files.
|
|
379
|
+
|
|
380
|
+
---
|
|
381
|
+
|
|
382
|
+
## 📄 License
|
|
383
|
+
|
|
384
|
+
MIT
|