Skip to content

Commit 7de4754

Browse files
committed
Update readme
1 parent 210451b commit 7de4754

File tree

1 file changed

+59
-38
lines changed

1 file changed

+59
-38
lines changed

README.md

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
[![pub package](https://img.shields.io/pypi/v/django-vitevue)](https://pypi.org/project/django-vitevue/)
44

5-
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
76

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
1111

1212
## Install
1313

@@ -17,54 +17,49 @@ commands are available:
1717

1818
Add `"vv",` to `INSTALLED_APPS`
1919

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
2122

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
2924

30-
STATICFILES_DIRS = [
31-
BASE_DIR / "static",
32-
]
25+
### Architecture and settings
3326

34-
STATIC_URL = "/static/"
35-
```
27+
The recommended file structure for a single page app is:
3628

37-
## Configuration of a Vitejs app
29+
- project_root_dir
30+
- django_project
31+
- vite_project
3832

3933
A management command is available to configure some Vitejs frontends compilation options
4034
and commands. First create a frontend in the parent folder of the Django project with a command
4135
like:
4236

43-
```
37+
```bash
4438
yarn create vite frontend --template=vue-ts
4539
```
4640

47-
### Settings
41+
Or use and existing one.
4842

4943
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:
5146

52-
```
53-
VV_BASE_DIR = Path("/some/directory")
47+
```python
48+
VV_BASE_DIR = BASE_DIR
5449
```
5550

5651
### Generate the Vitejs config
5752

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:
5954

6055
```
61-
python {project_name}/manage.py viteconf
56+
python {django_project}/manage.py viteconf
6257
```
6358

6459
Otherwise add the app folder name as an argument:
6560

6661
```
67-
python {project_name}/manage.py viteconf --app=my_frontend_app_folder_name
62+
python {django_project}/manage.py viteconf --app=my_frontend_app_folder_name
6863
```
6964

7065
This command will do the following things:
@@ -77,18 +72,26 @@ The command runs in dry mode and outputs the config. To write to config files
7772
use the `-w` flag:
7873

7974
```
80-
python {project_name}/manage.py viteconf -w
75+
python {django_project}/manage.py viteconf -w
8176
```
8277

83-
### Configure templates and static files destination
78+
### Options
79+
80+
#### Configure templates and static files destination
8481

8582
The npm *build* command will be configured to output to the Django static file
8683
folder and an *index.html* template. To change these options use these command flags:
8784

8885
`--template=mytemplate.html`: the template to compile to. Relative to the django templates dir
8986
`--static=myfolder`: the static folder to compile assets to. Relative to the first staticfiles dir
9087

91-
### Compile to a partial template
88+
Example to compile the template to `templates/myapp/mytemplate.html` and the static assets to `static/myapp`:
89+
90+
```
91+
python {django_project}/manage.py viteconf --template=myapp/mytemplate.html --static=myapp
92+
```
93+
94+
#### Compile to a partial template
9295

9396
By default it will compile a full index page, in single page app mode. It is possible to
9497
compile to a static partial template, without the html tags. Use the partial flag:
@@ -98,21 +101,23 @@ compile to a static partial template, without the html tags. Use the partial fla
98101
To configure Vitejs to compile an app in partial mode to a specific template and static folder:
99102

100103
```
101-
python {project_name}/manage.py viteconf -p --app=partialapp --template=mytemplate.html --static=myfolder
104+
python {django_project}/manage.py viteconf -p --app=partialapp --template=mytemplate.html --static=myfolder
102105
```
103106

104-
## Generate Typescript models
107+
## Typescript models
108+
109+
### Generate Typescript models from Django models
105110

106111
The `tsmodels` command can generate Typescript models from Django models:
107112

108113
```
109-
python {project_name}/manage.py tsmodels my_django_app
114+
python {django_project}/manage.py tsmodels my_django_app
110115
```
111116

112117
To write the models to the frontend app:
113118

114119
```
115-
python {project_name}/manage.py tsmodels my_django_app -w
120+
python {django_project}/manage.py tsmodels my_django_app -w
116121
```
117122

118123
<details>
@@ -240,14 +245,17 @@ export default interface TradeContract {
240245
</p>
241246
</details>
242247

243-
## Add an api to the generated frontend models
248+
### Add an api to the generated frontend models
244249

245250
To scaffold an api for an existing frontend model:
246251

247252
```
248-
python {project_name}/manage.py tsapi my_django_app_name
253+
python {django_project}/manage.py tsapi my_django_app_name
249254
```
250255

256+
This will create an api for the Typescript models and copy an `api` helper
257+
in the frontend `src` directory
258+
251259
<details>
252260
<summary>Example output</summary>
253261

@@ -264,10 +272,23 @@ export default class Market {
264272
}
265273
```
266274

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-
269275
</details>
270276

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+
271292
## Example
272293

273294
Example repository: https://github.com/synw/django-vitevue-example

0 commit comments

Comments
 (0)