-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add docs and update templates for Django-React integration #84
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, @jeancochrane! Excited to bring this in. Left a few things for your attention inline.
django/django-react-integration.md
Outdated
|
||
### Install Node packages | ||
|
||
Using `yarn add` or `npm install`, Update your `package.json` file to include |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason not to standardized on yarn
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I can think of. I removed the note about npm install
.
django/django-react-integration.md
Outdated
COMPRESS_OUTPUT_DIR = 'compressor' | ||
``` | ||
|
||
### Set up caching |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to be doing some optimization work along these lines for BGA next month. Happy to expand on this section then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
@@ -155,16 +154,11 @@ | |||
|
|||
# Django Compressor configs | |||
COMPRESS_PRECOMPILERS = ( | |||
('module', 'compressor_toolkit.precompilers.ES6Compiler'), | |||
('module', 'npx browserify {infile} -t [ babelify --presets [ @babel/preset-env ] ] > {outfile}'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 😄
|
||
There are many ways of integrating Django and React. At DataMade, our preferred | ||
method is a so-called "hybrid" approach, where Django builds React components into its templates | ||
by using a context variable for props and bundling components with Babel, Browserify, and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful to very succinctly describe what each part of this stack does, e.g., Django feeds the data to a React component in the template, Babel translates modern JavaScript for backwards compatibility, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this idea a lot. Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯!!
@@ -11,6 +11,7 @@ In some cases, it also provides extended documentation for setup and use. | |||
| CMS | [`wagtail`](https://wagtail.io/developers/) | [Link](wagtail/README.md) | | |||
| Search | [`django-haystack`](https://github.com/django-haystack/django-haystack) | | | |||
| Autocomplete | [`django-autocomplete-light`](https://github.com/yourlabs/django-autocomplete-light) | | | |||
| Cross-browser ES6 support | [`django-compressor`](https://github.com/django-compressor/django-compressor) + [`django-compressor-toolkit`](https://github.com/kottenator/django-compressor-toolkit) | [Link](django-compressor.md) | | |||
| Cross-browser ES6 support | [`django-compressor`](https://github.com/django-compressor/django-compressor) | [Link](django-compressor.md) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should point to the Django React documentation instead of linking to this file, or break out the setup portion of Django React into this compressor docs file. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I favor breaking installation out into the django-compressor.md
docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent point! I moved the installation instructions over to django-compressor.md
django/django-react-integration.md
Outdated
#### Bundle and load your component with `django-compressor` | ||
|
||
Finally, use the `compress` templatetag provided by `django-compressor` and the | ||
`view.component` attribute to bundle and load your component as an external script. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does view.component
come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's set on the view for this template in the section Define a view with a path to your component
above:
Define a Django view that includes a
component
attribute with a staticfile
path to the component you've defined, as well as the typicaltemplate_name
attribute representing the path to a Django template. The combination of these
two attributes will allow us to render the component in the view's template.class Home(TemplateView): title = 'Home' template_name = 'my_new_app/index.html' component = 'js/pages/index.js' # Staticfile path to your component code
I'll adjust the wording here to make it a little bit clearer where view.component
is coming from.
- Fix "template tag" typo - Change app name in django-react template Co-authored-by: hannah cushman garland <[email protected]>
…t integration overview
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the excellent comments @hancush! Back to you.
@@ -11,6 +11,7 @@ In some cases, it also provides extended documentation for setup and use. | |||
| CMS | [`wagtail`](https://wagtail.io/developers/) | [Link](wagtail/README.md) | | |||
| Search | [`django-haystack`](https://github.com/django-haystack/django-haystack) | | | |||
| Autocomplete | [`django-autocomplete-light`](https://github.com/yourlabs/django-autocomplete-light) | | | |||
| Cross-browser ES6 support | [`django-compressor`](https://github.com/django-compressor/django-compressor) + [`django-compressor-toolkit`](https://github.com/kottenator/django-compressor-toolkit) | [Link](django-compressor.md) | | |||
| Cross-browser ES6 support | [`django-compressor`](https://github.com/django-compressor/django-compressor) | [Link](django-compressor.md) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent point! I moved the installation instructions over to django-compressor.md
django/django-react-integration.md
Outdated
|
||
### Install Node packages | ||
|
||
Using `yarn add` or `npm install`, Update your `package.json` file to include |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I can think of. I removed the note about npm install
.
|
||
There are many ways of integrating Django and React. At DataMade, our preferred | ||
method is a so-called "hybrid" approach, where Django builds React components into its templates | ||
by using a context variable for props and bundling components with Babel, Browserify, and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this idea a lot. Done!
django/django-react-integration.md
Outdated
COMPRESS_OUTPUT_DIR = 'compressor' | ||
``` | ||
|
||
### Set up caching |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
django/django-react-integration.md
Outdated
#### Bundle and load your component with `django-compressor` | ||
|
||
Finally, use the `compress` templatetag provided by `django-compressor` and the | ||
`view.component` attribute to bundle and load your component as an external script. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's set on the view for this template in the section Define a view with a path to your component
above:
Define a Django view that includes a
component
attribute with a staticfile
path to the component you've defined, as well as the typicaltemplate_name
attribute representing the path to a Django template. The combination of these
two attributes will allow us to render the component in the view's template.class Home(TemplateView): title = 'Home' template_name = 'my_new_app/index.html' component = 'js/pages/index.js' # Staticfile path to your component code
I'll adjust the wording here to make it a little bit clearer where view.component
is coming from.
@@ -155,16 +154,11 @@ | |||
|
|||
# Django Compressor configs | |||
COMPRESS_PRECOMPILERS = ( | |||
('module', 'compressor_toolkit.precompilers.ES6Compiler'), | |||
('module', 'npx browserify {infile} -t [ babelify --presets [ @babel/preset-env ] ] > {outfile}'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks awesome, @jeancochrane! Thank you so much for your work on this. :-)
Overview
This PR adds documentation and updates the
new-django-app
template to recommend a pattern for using React components in a Django app.Connects #66.
Testing Instructions
docker/templates
directory and create a new app withdocker-compose run --rm cookiecutter -f new-django-app
my-new-app
repo and build container images withdocker-compose build
Usage
section of the new docs and use the example code to test the hybrid integrationWelcome, foobar!
belowdocker-compose down --volumes
cd ../ && rm -Rf my-new-app
docker rmi my-new-app