@@ -39,13 +39,17 @@ To integrate IDOM into your application you'll need to modify or add the followi
39
39
40
40
```
41
41
your_app/
42
+ ├── __init__.py
42
43
├── asgi.py
43
- ├── components.py
44
- ├── idom.py
45
44
├── settings.py
46
- ├── templates/
47
- │ ├── your-template.html
48
- └── urls.py
45
+ ├── urls.py
46
+ └── sub_app/
47
+ ├── __init__.py
48
+ ├── components.py
49
+ ├── idom.py
50
+ ├── templates/
51
+ │ └── your-template.html
52
+ └── urls.py
49
53
```
50
54
51
55
## ` asgi.py `
@@ -54,7 +58,7 @@ To start, we'll need to use [`channels`](https://channels.readthedocs.io/en/stab
54
58
create a ` ProtocolTypeRouter ` that will become the top of our ASGI application stack.
55
59
Under the ` "websocket" ` protocol, we'll then add a path for IDOM's websocket consumer
56
60
using ` idom_websocket_path ` . If you wish to change the route where this
57
- websocket is served from see the [ settings] ( #configuration-options ) .
61
+ websocket is served from see the available [ settings] ( #settings.py ) .
58
62
59
63
``` python
60
64
@@ -82,7 +86,44 @@ application = ProtocolTypeRouter(
82
86
)
83
87
```
84
88
85
- ## ` components.py `
89
+ ## ` settings.py `
90
+
91
+ In your settings you'll need to add ` django_idom ` to the
92
+ [ ` INSTALLED_APPS ` ] ( https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-INSTALLED_APPS )
93
+ list:
94
+
95
+ ``` python
96
+ INSTALLED_APPS = [
97
+ ... ,
98
+ " django_idom" ,
99
+ ]
100
+ ```
101
+
102
+ You may configure additional options as well:
103
+
104
+ ``` python
105
+ # the base URL for all IDOM-releated resources
106
+ IDOM_BASE_URL : str = " _idom/"
107
+
108
+ # ignore these INSTALLED_APPS during component collection
109
+ IDOM_IGNORE_INSTALLED_APPS : list[str ] = [" some_app" , " some_other_app" ]
110
+ ```
111
+
112
+ ## ` urls.py `
113
+
114
+ You'll need to include IDOM's static web modules path using ` idom_web_modules_path ` .
115
+ Similarly to the ` idom_websocket_path() ` , these resources will be used globally.
116
+
117
+ ``` python
118
+ from django_idom import idom_web_modules_path
119
+
120
+ urlpatterns = [
121
+ idom_web_modules_path(),
122
+ ...
123
+ ]
124
+ ```
125
+
126
+ ## ` sub_app/components.py `
86
127
87
128
This is where, by a convention similar to that of
88
129
[ ` views.py ` ] ( https://docs.djangoproject.com/en/3.2/topics/http/views/ ) , you'll define
@@ -97,35 +138,36 @@ def Hello(name): # component names are camelcase by convention
97
138
return idom.html.h1(f " Hello { name} ! " )
98
139
```
99
140
100
- ## ` idom.py `
141
+ ## ` sub_app/ idom.py`
101
142
102
143
This file is automatically discovered by ` django-idom ` when scanning the list of
103
144
[ ` INSTALLED_APPS ` ] ( https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-INSTALLED_APPS ) .
104
145
All apps that export components will contain this module.
105
146
106
147
Inside this module must be a ` components ` list that is imported from
107
- [ ` components.py ` ] ( #components .py ) :
148
+ [ ` components.py ` ] ( #sub_appcomponents .py ) :
108
149
109
150
``` python
110
151
from .components import Hello
111
152
112
- components = [Hello]
153
+ components = [
154
+ Hello,
155
+ ...
156
+ ]
113
157
```
114
158
115
- ## ` settings.py `
116
-
117
- In your settings you'll need to add ` django_idom ` to the
118
- [ ` INSTALLED_APPS ` ] ( https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-INSTALLED_APPS )
119
- list:
159
+ You may alternately reference the components with strings for the purpose of renaming:
120
160
121
161
``` python
122
- INSTALLED_APPS = [
123
- ... ,
124
- " django_idom" ,
162
+ from .components import Hello as SomeOtherName
163
+
164
+ components = [
165
+ " SomeOtherName" ,
166
+ ...
125
167
]
126
168
```
127
169
128
- ## ` templates/your-template.html `
170
+ ## ` sub_app/ templates/your-template.html`
129
171
130
172
In your templates, you may inject a view of an IDOM component into your templated HTML
131
173
by using the ` idom_view ` template tag. This tag which requires the name of a component
@@ -147,41 +189,40 @@ In context this will look a bit like the following...
147
189
<html >
148
190
<body >
149
191
...
150
- {% idom_view "test_app .Hello" name="World" %}
192
+ {% idom_view "your_app.sub_app .Hello" name="World" %}
151
193
</body >
152
194
</html >
153
195
```
154
196
155
197
Your view for this template can be defined just
156
198
[ like any other] ( https://docs.djangoproject.com/en/3.2/intro/tutorial03/#write-views-that-actually-do-something ) .
157
199
158
- ## ` urls.py `
159
-
160
- To your list of URLs you'll need to include IDOM's static web modules path using
161
- ` idom_web_modules_path ` :
200
+ ## ` sub_app/views.py `
162
201
163
202
``` python
164
- from django.urls import path
165
- from django_idom import idom_web_modules_path
166
- from .views import your_template # define this view like any other HTML template
203
+ from django.http import HttpResponse
204
+ from django.template import loader
167
205
168
206
169
- urlpatterns = [
170
- path(" " , your_template),
171
- idom_web_modules_path(),
172
- ]
207
+ def your_template (request ):
208
+ context = {}
209
+ return HttpResponse(
210
+ loader.get_template(" your-template.html" ).render(context, request)
211
+ )
173
212
```
174
213
175
- # Configuration Options
214
+ ## ` sub_app/urls.py `
176
215
177
- You may configure additional options in your ` settings.py ` file
216
+ Include your replate in the list of urlpatterns
178
217
179
218
``` python
180
- # the base URL for all IDOM-releated resources
181
- IDOM_BASE_URL : str = " _idom/ "
219
+ from django.urls import path
220
+ from .views import your_template # define this view like any other HTML template
182
221
183
- # ignore these INSTALLED_APPS during component collection
184
- IDOM_IGNORE_INSTALLED_APPS : set[str ] = {" some_app" , " some_other_app" }
222
+ urlpatterns = [
223
+ path(" " , your_template),
224
+ ...
225
+ ]
185
226
```
186
227
187
228
# Developer Guide
0 commit comments