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
Copy file name to clipboardExpand all lines: README.md
+31-29Lines changed: 31 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -185,25 +185,23 @@ React assets will be included with the other static assets in the `settings.STAT
185
185
186
186
Similar to the `bundle_js` template variable mentioned earlier, **django-cra-helper** includes numerous other template variables when the CRA liveserver is _not_ running:
187
187
188
-
<details>
189
-
<summary>For older projects using <strong>react-scripts@<=2.1.8</strong></summary>
188
+
<detailsopen>
189
+
<summary>For projects using <strong>react-scripts@>=3.2.0</strong></summary>
190
190
191
-
The two most important variables are `main_js` and `main_css`. These can be injected into the page via a typical call to `{% static %}` in the template:
191
+
Starting with `[email protected]`, a new `entrypoints` property can be found in **asset-manifest.json**. This contains an array of files that **django-cra-helper** makes available in templates to more easily inject these files via new `entrypoints.css` and `entrypoints.js` arrays:
> NOTE: Recent attempts at building a fresh CRA project with `[email protected]` were unsuccessful in recreating SPAs that allowed for just a single `main_js`. `npm run build`-produced artifacts functioned almost identically to artifacts generated the same as `[email protected]`, detailed below.
205
-
>
206
-
> There may be child dependencies of `react-scripts` that make it no longer possible to start apps that will function with the above instructions. In these cases, please try the instructions in the next section.
204
+
> NOTE: These JavaScript and CSS files should be arranged in an order required for the site to load; the ultimate order is derived from the order present in **asset-manifest.json**.
207
205
</details>
208
206
209
207
<details>
@@ -223,22 +221,24 @@ Similar to the `bundle_js` template variable mentioned earlier, **django-cra-hel
223
221
</details>
224
222
225
223
<details>
226
-
<summary>For projects using <strong>react-scripts@>=3.2.0</strong></summary>
224
+
<summary>For older projects using <strong>react-scripts@<=2.1.8</strong></summary>
227
225
228
-
Starting with `[email protected]`, a new `entrypoints` property can be found in **asset-manifest.json**. This contains an array of files that **django-cra-helper** makes available in templates to more easily inject these files via new `entrypoints.css` and `entrypoints.js` arrays. These **replace**the `main_css` and `main_js` values used above:
226
+
The two most important variables are `main_js` and `main_css`. These can be injected into the page via a typical call to `{% static %}` in the template:
> NOTE: These JavaScript and CSS files should be arranged in an order required for the site to load; the ultimate order is derived from the order present in **asset-manifest.json**.
239
+
> NOTE: Recent attempts at building a fresh CRA project with `[email protected]` were unsuccessful in recreating SPAs that allowed for just a single `main_js`. `npm run build`-produced artifacts functioned almost identically to artifacts generated the same as `[email protected]`, detailed below.
240
+
>
241
+
> There may be child dependencies of `react-scripts` that make it no longer possible to start apps that will function with the above instructions. In these cases, please try the instructions in the next section.
**To make sure the React imports/assets/etc... can be found even when hosted through Django, you'll also need to update `STATIC_URL` in Django's settings.py to include the path prefix:**
264
+
To make sure the React imports/assets/etc... can be found even when hosted through Django, you'll also need to update `STATIC_URL` in Django's settings.py to include the path prefix:
265
265
266
266
```py
267
267
STATIC_URL='/frontend/static/'
268
+
CRA_PACKAGE_JSON_HOMEPAGE='/frontend'
268
269
```
269
270
271
+
The value set to `CRA_PACKAGE_JSON_HOMEPAGE` above should match the value of `"homepage"` in **package.json** so that **django-cra-helper** can find the CRA liveserver and redirect appropriately:
272
+
270
273
Once these changes are made then the React app should be able to find everything it needs to function.
271
274
272
275
## React in Django templates
@@ -332,7 +335,6 @@ Below is the Django app view's **index.html** template that can render across mu
332
335
333
336
```html
334
337
{% load static %}
335
-
{% load cra_helper_tags %}
336
338
<!DOCTYPE html>
337
339
<htmllang="en">
338
340
@@ -353,9 +355,13 @@ Below is the Django app view's **index.html** template that can render across mu
@@ -378,12 +384,8 @@ Below is the Django app view's **index.html** template that can render across mu
378
384
```
379
385
The context's `component` and `props` are bound to `window.component` and `window.props` respectively.
380
386
381
-
Note the use of the `json` filter when setting `windows.props`! `{% load cra_helper_tags %}`provides this filter as a way to easily sanitize and convert a Python `dict` to a Javascript `Object`. The View context prepared above thus renders to the following typical Javascript Object:
387
+
Note the use of the `json_script` filter when setting `windows.props`. [Django provides this filter](https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#json-script) as a way to easily sanitize and convert a Python `dict` to a Javascript `Object`. The contents of the injected `<script>` tag can be run through `JSON.parse()`to safely assign it to a variable.
382
388
383
-
```js
384
-
// This is what is returned in the rendered HTML
385
-
window.props= {"env":"Django"};
386
-
```
387
389
Finally, `window.reactRoot` specifies the container element that the React component should be rendered into. Setting a value for this is only required if the container's `id` is *not***"root"** (the same ID assigned to the container `<div>` in the CRA project's `index.html`.)
0 commit comments