Aravind
Aravind

Reputation: 91

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

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.

Upvotes: 6

Views: 2704

Answers (2)

Dap
Dap

Reputation: 2359

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 applications 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 serve the one page app from there. 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

Upvotes: 10

Danil
Danil

Reputation: 5181

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.

Upvotes: 1

Related Questions