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
Manage Vitejs frontends and compile them to Django static files and templates. Some management
6
-
commands are available:
5
+
Manage Vitejs frontends and compile them to Django static files and templates. Features
7
6
8
-
-[viteconf](#configuration-of-a-vitejs-app): generate a Vitejs compilation configuration
9
-
-[tsmodels](#generate-typescript-models): generate Typescript models from Django models
10
-
-[tsapi](#add-an-api-to-the-generated-frontend-models): scaffold an api for the generated frontend models
7
+
-[Configure Vitejs for Django](#configuration-of-a-vitejs-app-to-compile-to-django-templates-and-static-files): use a management
8
+
command to help configuring Vitejs to compile to Django templates and static files
9
+
-[Typescript scaffolding](#typescript-models): generate Typescript models from existing Django models
10
+
-[Api and views](#add-an-api-to-the-generated-frontend-models): api helper frontend class configured for Django and login/logout views with single page app support
11
11
12
12
## Install
13
13
@@ -17,54 +17,49 @@ commands are available:
17
17
18
18
Add `"vv",` to `INSTALLED_APPS`
19
19
20
-
Make sure these basic Django settings are present:
20
+
Make sure the basic Django template and static dirs settings are present. Run the
21
+
`vvcheck` management command to see if the config is ok
21
22
22
-
```python
23
-
TEMPLATES= [
24
-
{
25
-
"DIRS": [BASE_DIR/"templates"],
26
-
# ...
27
-
},
28
-
]
23
+
## Configuration of a Vitejs app to compile to Django templates and static files
29
24
30
-
STATICFILES_DIRS= [
31
-
BASE_DIR/"static",
32
-
]
25
+
### Architecture and settings
33
26
34
-
STATIC_URL="/static/"
35
-
```
27
+
The recommended file structure for a single page app is:
36
28
37
-
## Configuration of a Vitejs app
29
+
- project_root_dir
30
+
- django_project
31
+
- vite_project
38
32
39
33
A management command is available to configure some Vitejs frontends compilation options
40
34
and commands. First create a frontend in the parent folder of the Django project with a command
41
35
like:
42
36
43
-
```
37
+
```bash
44
38
yarn create vite frontend --template=vue-ts
45
39
```
46
40
47
-
### Settings
41
+
Or use and existing one.
48
42
49
43
The root directory can be configured by a setting. By default it is
50
-
the parent directory of the Django's BASE_DIR. Example setting:
44
+
the parent directory of the Django's `BASE_DIR`, like in the file structure shown above.
45
+
Example setting to put the frontend dev code directly in the django project directory:
51
46
52
-
```
53
-
VV_BASE_DIR = Path("/some/directory")
47
+
```python
48
+
VV_BASE_DIR=BASE_DIR
54
49
```
55
50
56
51
### Generate the Vitejs config
57
52
58
-
If the folder is named *frontend* the command can run without arguments:
53
+
If the Vite app project folder is named *frontend* the command can run without arguments:
This will create an api for the Typescript models and copy an `api` helper
257
+
in the frontend `src` directory
258
+
251
259
<details>
252
260
<summary>Example output</summary>
253
261
@@ -264,10 +272,23 @@ export default class Market {
264
272
}
265
273
```
266
274
267
-
<p>The command will create an api directory containing an helper class: https://github.com/synw/django-vitevue/blob/master/vv/files/api/model.ts</p>
268
-
269
275
</details>
270
276
277
+
### Login views
278
+
279
+
Some login/logout views are available from the backend, and supported by the frontend
280
+
api helper class. Add the urls in `urls.py`:
281
+
282
+
```
283
+
urlpatterns = [
284
+
path("vv/", include("vv.urls")),
285
+
#...
286
+
]
287
+
```
288
+
289
+
Two api views will be available: `/vv/auth/login/` and `/vv/auth/logout/`. The frontend api
290
+
helper class have support for these views [example code](https://github.com/synw/django-vitevue-example/blob/main/django_vitevue_example/static/demo/App.vue)
291
+
271
292
## Example
272
293
273
294
Example repository: https://github.com/synw/django-vitevue-example
0 commit comments