Home > Enterprise >  Integration of Front End and Backend
Integration of Front End and Backend

Time:06-01

I am planning on creating a React/Django web application. I plan on creating an API on the backend to communicate with my React app. I understand you can integrate React with django by including your react app in the django project directory and pointing to the index.html in react.

I'm still not sure what the benefits of this are. Would it be better just to separate the django and react application. Whenever I would need to store or retrieve data I could just make requests the django API?

CodePudding user response:

The better is to separate the two projects because a lot of reasons, The main reason is when you need to build a mobile or desktop application on the same backend or database here comes the advantage of APIs, there are a lot of reasons such as: separate the servers to handle requests of both frontend (static files) and the backend (APIs and DB).

CodePudding user response:

The best approach is to separate, and use DRF (Django Rest Framework), this library will offer serializers which basically going to interpret your request and then process it in a view class/function in Django, the whole process goes like this (between frontend and backend):

  1. You create the tables in database (models).

  2. You create the views, and some views are going to retrieve data from the serializer, to process the request being sent from client side (react).

  3. You map the views to urls.

Now react part:

  1. You make some api calls.

  2. The request is sent to backend (in this case it is django).

  3. The request is sent as "JSON" data format, but python does not understand such format, this is why serializers exist! the serializer will interpret the data and convert it to something that python understand (a dictionary), and then the request is going to be processed by a view.

  4. A response will be sent to react.

Hope this clear it for you, for more info about DRF:

https://www.django-rest-framework.org/

also serializers can also change the lookup field for some resource in your backend, which is pretty useful when you want to look up for name instead of an id in the url

more info about serializers:

https://www.django-rest-framework.org/community/third-party-packages/#serializers

  • Related