@@ -25,17 +25,18 @@ by Python dotted path in `your-template.html`.
25
25
from idom import component, html
26
26
from django_idom import IdomWebsocket
27
27
28
-
28
+ # Components are CamelCase by ReactJS convention
29
29
@component
30
- def Hello (websocket : IdomWebsocket, greeting_recipient : str ): # Names are CamelCase by ReactJS convention
30
+ def Hello (websocket : IdomWebsocket, greeting_recipient : str ):
31
31
return html.header(f " Hello { greeting_recipient} ! " )
32
32
```
33
33
34
34
## [ ` example_app/templates/your-template.html ` ] ( https://docs.djangoproject.com/en/dev/topics/templates/ )
35
35
36
36
In your templates, you may add IDOM components into your HTML by using the ` idom_component `
37
- template tag. This tag requires the dotted path to the component function. Additonally, you can
38
- pass in keyworded arguments into your component function.
37
+ template tag. This tag requires the dotted path to the component function.
38
+
39
+ Additonally, you can pass in keyworded arguments into your component function.
39
40
40
41
In context this will look a bit like the following...
41
42
@@ -44,8 +45,11 @@ In context this will look a bit like the following...
44
45
45
46
<!DOCTYPE html>
46
47
<html>
48
+ <head>
49
+ <title>Example Project</title>
50
+ </head>
51
+
47
52
<body>
48
- ...
49
53
{% idom_component "my_django_project.example_app.components.Hello" greeting_recipient="World" %}
50
54
</body>
51
55
</html>
@@ -59,27 +63,27 @@ Install `django-idom` via pip.
59
63
pip install django-idom
60
64
```
61
65
62
- ---
63
-
64
- You'll also need to modify a few files in your Django project...
66
+ You'll also need to modify a few files in your Django project.
65
67
66
68
## [ ` settings.py ` ] ( https://docs.djangoproject.com/en/dev/topics/settings/ )
67
69
68
- In your settings you'll need to add ` django_idom ` to the
69
- [ ` INSTALLED_APPS ` ] ( https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-INSTALLED_APPS )
70
- list:
70
+ In your settings you'll need to add ` channels ` and ` django_idom ` to [ ` INSTALLED_APPS ` ] ( https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-INSTALLED_APPS ) .
71
71
72
72
``` python
73
73
INSTALLED_APPS = [
74
74
... ,
75
+ " channels" ,
75
76
" django_idom" ,
76
77
]
78
+
79
+ # Ensure ASGI_APPLICATION is set properly based on your project name!
80
+ ASGI_APPLICATION = " my_django_project.asgi.application"
77
81
```
78
82
79
- You may configure additional options as well.. .
83
+ ** Optional: ** You can also configure IDOM settings .
80
84
81
85
``` python
82
- # If "idom" cache is not configured, then we'll use the "default" instead
86
+ # If "idom" cache is not configured, then we'll use "default" instead
83
87
CACHES = {
84
88
" idom" : {" BACKEND" : ... },
85
89
}
@@ -88,15 +92,17 @@ CACHES = {
88
92
# 0 will disable reconnection.
89
93
IDOM_WS_MAX_RECONNECT_TIMEOUT : int = 604800
90
94
91
- # The URL for IDOM to serve its Websockets
95
+ # The URL for IDOM to serve websockets
92
96
IDOM_WEBSOCKET_URL : str = " idom/"
93
97
```
94
98
95
99
## [ ` urls.py ` ] ( https://docs.djangoproject.com/en/dev/topics/http/urls/ )
96
100
97
- Add Django- IDOM http URLs to your ` urlpatterns ` .
101
+ Add IDOM HTTP paths to your ` urlpatterns ` .
98
102
99
103
``` python
104
+ from django.urls import include, path
105
+
100
106
urlpatterns = [
101
107
path(" idom/" , include(" django_idom.http.urls" )),
102
108
...
@@ -105,29 +111,27 @@ urlpatterns = [
105
111
106
112
## [ ` asgi.py ` ] ( https://docs.djangoproject.com/en/dev/howto/deployment/asgi/ )
107
113
108
- If you do not have an ` asgi.py ` , first follow the [ ` channels ` installation guide] ( https://channels.readthedocs.io/en/stable/installation.html ) in
109
- order to create websockets within Django.
110
-
111
- We will add IDOM's websocket consumer path using ` IDOM_WEBSOCKET_PATH ` .
114
+ Register IDOM's websocket using ` IDOM_WEBSOCKET_PATH ` .
112
115
113
- _ Note: If you wish to change the route where this websocket is served from, see the
114
- available [ settings] ( #settingspy ) ._
116
+ _ Note: If you do not have an ` asgi.py ` , follow the [ ` channels ` installation guide] ( https://channels.readthedocs.io/en/stable/installation.html ) ._
115
117
116
118
``` python
117
119
118
120
import os
119
121
from django.core.asgi import get_asgi_application
120
- from django_idom import IDOM_WEBSOCKET_PATH
121
122
122
- os.environ.setdefault(" DJANGO_SETTINGS_MODULE" , " test_app.settings" )
123
- http_asgi_app = get_asgi_application()
123
+ # Ensure DJANGO_SETTINGS_MODULE is set properly based on your project name!
124
+ os.environ.setdefault(" DJANGO_SETTINGS_MODULE" , " my_django_project.settings" )
125
+ django_asgi_app = get_asgi_application()
124
126
125
127
from channels.auth import AuthMiddlewareStack
126
128
from channels.routing import ProtocolTypeRouter, URLRouter
129
+ from channels.sessions import SessionMiddlewareStack
130
+ from django_idom import IDOM_WEBSOCKET_PATH
127
131
128
132
application = ProtocolTypeRouter(
129
133
{
130
- " http" : http_asgi_app ,
134
+ " http" : django_asgi_app ,
131
135
" websocket" : SessionMiddlewareStack(
132
136
AuthMiddlewareStack(URLRouter([IDOM_WEBSOCKET_PATH ]))
133
137
),
0 commit comments