Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the options to configure a reactjs and django application?

Tags:

reactjs

django

I am using reactjs for frontend and Django REST API for the backened. What is the best possible way to integrate both? which of these two is a good option?

  1. Running two servers for frontend and backend?

or

  1. replacing django templates with reactjs?

Your help is highly appreciated.

like image 779
Aravind Avatar asked May 11 '18 15:05

Aravind


People also ask

How do you integrate Django and Reactjs?

Open up the settings.py file and make the following changes. then configure the template directory by pointing it towards the build folder in our frontend react directory. We need to configure the static files directory as well; add this line to the bottom of the page below the STATIC_URL line.

Do React and Django work well together?

Although Django and React can play well together, there's been a lot of debate in recent years about how far to push the React model of application development. Some think that React has introduced too much complexity into web development and is now overused.


2 Answers

Few options here

Django templates with react. Not my preferred method. Essentially, you are blending django templating and jsx. The benefit here is low over head. It requires little configuration and allows you to write react and leverage the django templating language in the same file. If you need to get something up and running quickly, its a great solution. Have a look at this library https://github.com/Frojd/django-react-templatetags

Using django webpack loader This will allow you to separate your react code from django code but still keep all your code to one repository. You need to configure django settings to find your react code. Then on your prod/dev server, have your web server point to the directory where your static react code lives or write a django url and view that will serve the react apps index file. It will be located in /static/ after you configure correctly and run python manage.py collectstatics. Benefits here are that it keeps the code to one repository but still isolates the python and javascript code. This is a middleground solution of the three. Quick note. You won't have react hot reload with this method for development. Here is the library that helps you configure this setup https://github.com/owais/django-webpack-loader

Having 2 separate repositories Similar to what you are doing right now. Have a separate react repository, either served by a nodejs backend or deploy the code to a cdn service like amazon s3 and have amazon serve the one page app. And then as its counterpart, have your django app on a separate server with its consumable rest api (will need to configure allowed cors) . This method requires a lot of operational work: deploying, configuring, and management of 2 separate code bases. If you have the time and resources I do recommend this setup. The decoupling of the 2 apps allows this solution to scale the best

like image 115
Dap Avatar answered Nov 15 '22 03:11

Dap


What do you mean two servers? You mean two projects/repositories? Yes, you can keep frontend in the separate project. It make sence if you have multiple clients for your backend (like mobile apps and web). Different developers can have permission to edit only their repositories. Also it make sence if you are going to use some microservices structure for your project. But more simpliest way is to keep frontend and backed in the same project. Try to check some tutorial about Django+reactjs apps.

like image 26
Daniil Mashkin Avatar answered Nov 15 '22 04:11

Daniil Mashkin