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
@@ -12,19 +12,50 @@ If you use **Jinja2 templates** in your Django project and want to add ReactPy i
12
12
13
13
---
14
14
15
-
## Step 1: Install `jinja2`
15
+
## Step 1: Install from PyPI
16
16
17
-
Jinja2 is a Python dependency you must install in your environment to use this feature.
17
+
Run the following command to install [`reactpy-django`](https://pypi.org/project/reactpy-django/) and [`jinja2`](https://pypi.org/project/Jinja2/)in your Python environment.
18
18
19
19
```bash linenums="0"
20
-
pip install jinja2
20
+
pip install reactpy-django jinja2
21
21
```
22
22
23
-
---
23
+
## Step 2: Configure `settings.py`
24
+
25
+
Add `#!python "reactpy_django"` to [`INSTALLED_APPS`](https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-INSTALLED_APPS) in your [`settings.py`](https://docs.djangoproject.com/en/stable/topics/settings/) file.
26
+
27
+
=== "settings.py"
28
+
29
+
```python
30
+
{% include "../../examples/python/configure_installed_apps.py" %}
31
+
```
32
+
33
+
??? warning "Enable ASGI and Django Channels (Required)"
34
+
35
+
ReactPy-Django requires Django ASGI and [Django Channels](https://github.com/django/channels) WebSockets.
36
+
37
+
If you have not enabled ASGI on your **Django project** yet, here is a summary of the [`django`](https://docs.djangoproject.com/en/stable/howto/deployment/asgi/) and [`channels`](https://channels.readthedocs.io/en/stable/installation.html) installation docs:
24
38
25
-
## Step 2: Configure the Jinja2 template engine
39
+
1. Install `channels[daphne]`
40
+
2. Add `#!python "daphne"` to `#!python INSTALLED_APPS`.
26
41
27
-
Add a Jinja2 backend entry to your `TEMPLATES` list in [`settings.py`](https://docs.djangoproject.com/en/stable/topics/settings/).
42
+
```python linenums="0"
43
+
{% include "../../examples/python/configure_channels_installed_app.py" %}
44
+
```
45
+
46
+
3. Set your `#!python ASGI_APPLICATION` variable.
47
+
48
+
```python linenums="0"
49
+
{% include "../../examples/python/configure_channels_asgi_app.py" %}
50
+
```
51
+
52
+
??? info "Configure ReactPy settings (Optional)"
53
+
54
+
ReactPy's has additional configuration available to fit a variety of use cases.
55
+
56
+
See the [ReactPy settings](../reference/settings.md) documentation to learn more.
57
+
58
+
Also add a Jinja2 backend entry to your `TEMPLATES` list:
Add ReactPy HTTP paths to your `#!python urlpatterns` in your [`urls.py`](https://docs.djangoproject.com/en/stable/topics/http/urls/) file.
91
+
92
+
=== "urls.py"
93
+
94
+
```python
95
+
{% include "../../examples/python/configure_urls.py" %}
96
+
```
97
+
98
+
## Step 4: Configure `asgi.py`
99
+
100
+
Register ReactPy's WebSocket using `#!python REACTPY_WEBSOCKET_ROUTE` in your [`asgi.py`](https://docs.djangoproject.com/en/stable/howto/deployment/asgi/) file.
101
+
102
+
=== "asgi.py"
103
+
104
+
```python
105
+
{% include "../../examples/python/configure_asgi.py" %}
106
+
```
68
107
69
-
## Step 3: Create a Jinja2 environment module
108
+
??? info "Add `#!python AuthMiddlewareStack` (Optional)"
109
+
110
+
There are many situations where you need to access the Django `#!python User` or `#!python Session` objects within ReactPy components. For example, if you want to:
111
+
112
+
1. Access the `#!python User` that is currently logged in
113
+
3. Access Django's `#!python Session` object
114
+
2. Login or logout the current `#!python User`
115
+
116
+
In these situations will need to ensure you are using `#!python AuthMiddlewareStack`.
117
+
118
+
{% include "../../includes/auth-middleware-stack.md" %}
119
+
120
+
??? question "Where is my `asgi.py`?"
121
+
122
+
If you do not have an `asgi.py`, follow the [`channels` installation guide](https://channels.readthedocs.io/en/stable/installation.html).
123
+
124
+
## Step 5: Register the Jinja2 Extension
70
125
71
126
Create a `jinja_env.py` module that registers the `ReactPyExtension`:
72
127
@@ -83,54 +138,79 @@ Create a `jinja_env.py` module that registers the `ReactPyExtension`:
83
138
return env
84
139
```
85
140
86
-
---
141
+
## Step 6: Run database migrations
87
142
88
-
## Step 4: Use ReactPy components in Jinja2 templates
143
+
Run Django's [`migrate` command](https://docs.djangoproject.com/en/stable/topics/migrations/) to initialize ReactPy-Django's database table.
89
144
90
-
With the extension registered, you can call ReactPy functions directly inside any Jinja2 template:
The [next page](./your-first-component.md) will show you how to create your first ReactPy component.
113
160
114
-
Unlike Django templates which require `{% load reactpy %}` and `{% component "..." %}`, Jinja2 allows you to call component functions directly using the `{{ component(...) }}` syntax. The registered functions are:
161
+
Prefer a quick summary? Read the **At a Glance** section below.
115
162
116
-
| Function | Description |
117
-
|---|---|---|
118
-
| `{{ component(dotted_path, *args, **kwargs) }}` | Render a server-side ReactPy component. Equivalent to `{% component %}`. |
119
-
| `{{ pyscript_component(*file_paths, initial, root) }}` | Render a client-side PyScript component. Equivalent to `{% pyscript_component %}`. |
Unlike Django templates which require `{% load reactpy %}` and `{% component "..." %}`, Jinja2 allows you to call component functions directly using the `{{ component(...) }}` syntax. The registered functions are:
135
211
136
-
Now you're ready to [create your first component](./your-first-component.md).
212
+
| Function | Description |
213
+
|---|---|
214
+
| `{{ component(dotted_path, *args, **kwargs) }}` | Render a server-side ReactPy component. Equivalent to `{% component ... %}`. |
215
+
| `{{ pyscript_component(*file_paths, initial, root) }}` | Render a client-side PyScript component. Equivalent to `{% pyscript_component ... %}`. |
0 commit comments