Skip to content
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

Merged
merged 4 commits into from
Jun 25, 2020

Conversation

jeancochrane
Copy link
Contributor

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

  • View the rendered markdown, confirm it looks good and links resolve properly
  • Change to the docker/templates directory and create a new app with docker-compose run --rm cookiecutter -f new-django-app
  • Accept all default Cookiecutter options
  • Change to the my-new-app repo and build container images with docker-compose build
  • Follow the instructions in the Usage section of the new docs and use the example code to test the hybrid integration
  • Visit http://localhost:8000 and confirm you see the text Welcome, foobar! below
  • Once you're done testing, remember to clean up the testing app:
    • docker-compose down --volumes
    • cd ../ && rm -Rf my-new-app
    • docker rmi my-new-app

@jeancochrane jeancochrane requested a review from hancush June 18, 2020 17:07
Copy link
Member

@hancush hancush left a 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.


### Install Node packages

Using `yarn add` or `npm install`, Update your `package.json` file to include
Copy link
Member

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?

Copy link
Contributor Author

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.

COMPRESS_OUTPUT_DIR = 'compressor'
```

### Set up caching
Copy link
Member

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.

Copy link
Contributor Author

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}'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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!

Copy link
Member

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) |
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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

#### 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.
Copy link
Member

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?

Copy link
Contributor Author

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 typical template_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.

jeancochrane and others added 3 commits June 25, 2020 10:56
Copy link
Contributor Author

@jeancochrane jeancochrane left a 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) |
Copy link
Contributor Author

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


### Install Node packages

Using `yarn add` or `npm install`, Update your `package.json` file to include
Copy link
Contributor Author

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
Copy link
Contributor Author

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!

COMPRESS_OUTPUT_DIR = 'compressor'
```

### Set up caching
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

#### 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.
Copy link
Contributor Author

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 typical template_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}'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 😄

@jeancochrane jeancochrane requested a review from hancush June 25, 2020 16:38
Copy link
Member

@hancush hancush left a 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. :-)

@jeancochrane jeancochrane merged commit 84f4229 into master Jun 25, 2020
@jeancochrane jeancochrane deleted the jfc/django-react-integration branch June 25, 2020 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants