Your plan should work, but I think you should think about how much your application you want to be dynamic.
If you only need a few pages for the dynamic functionality that comes from React, it would probably be nice to just spit on some of the standalone React components and bundle them separately as you planned. However, if most of your project should be dynamic, I think it would be better to forget about Django templates for most pages and just create a robust API for using React.
Here is a brief example of how you can configure webpack to combine your responsive components:
This is an excerpt from a large Django project in which we use a couple of React components. bundle
has tons of other javascript stuff, and report
is a standalone React component:
webpack.config.js
... entry: { bundle: './server/apps/core/static/core/app.js', report: './server/apps/reports/static/reports/js/app.jsx' }, output: { path: path.resolve('./server/apps/core/static/core/bundles'), path: path.resolve('./server/apps/core/static/core/bundles'), filename: 'bundle.js' + filename: '[name].js' }, resolve: { extensions: ['', '.js', '.jsx'] }, ...
And then in the template that you want to insert the component, it:
{% block content %} <div id="root"></div> {% endblock content %} {% block extrascripts %} <script type="text/javascript"> var dataUrl = "{% url 'reports:data-chart' %}"; </script> <script src="{% static 'core/bundles/report.js' %}"></script> {% endblock extrascripts %}
I have another example here that may be useful for the game. I need to update it, but it is more likely a template for using React with the Django REST Framework and comes before the Django template system.
Hope this helps.
source share