I've started off building this social media kind of website, which includes the basic social media pages such as a profile, a status feed, searching, events, and so on. Each page is a seperate django template and data is visualized by using variables in the django template engine. But at this point, I want to change these static pages to something more dynamic, using ReactJS.
I would like to make the pages dynamic, but it should still be a multi page website with urls like /user/<id>
. I came up with the following option:
A template for each page. Each page having one bundle. Let Django handle the routes.
webpack.config.js
. This can result in a fairly large amount of entry points when we have 40+ different pages.request.user
to a javascript variable accessible by React.With Django for backend and React for frontend, nothing can stop you from creating a brilliant web app.
React will consume your Django REST API. React will make HTTP requests to your REST API in order to fetch and set data. React, with the help of Webpack (module bundler) & Babel (transpiler), will bundle and transpile your Javascript into single or multiple files that will be placed in the entry HTML page.
We recommend going with Django for the backend and Reactjs for the frontend. The Django Rest framework will be used to connect the frontend (Reactjs) with the backend (Django). If you are more comfortable with Python or JavaScript, you can decide based on this.
You'll build the front end of your web app using Django templates, which the framework renders by request to HTML pages that your users will get to interact with.
Your plan should work, but I think you should consider how much of your application you want to be dynamic.
If you only need a few pages to have the dynamic functionality that comes from React, it's probably a good idea to just plop in some self-contained React components and bundle them separately as you have planned. However, if most of your project will need to be dynamic, I think it would be best to forget about Django templates for most of the pages, and just create a solid API backend for React to use.
Here is a quick example of how you can configure webpack to bundle up your react components:
This is an excerpt from a large Django project where we are using a couple React components. bundle
has a bunch of other javascript stuff, and report
is a self-contained 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 stick 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 might be helpful to play around with. I need to update it, but it is more of a boilerplate for using React with Django REST Framework, and foregoing the Django template system.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With