简体   繁体   English

如何连接 React 和 Django?

[英]How to connect React with Django?

My team has decided to use React for the frontend and Django for the backend.我的团队决定在前端使用 React,在后端使用 Django。

What is the correct way to link Django and React?链接 Django 和 React 的正确方法是什么? Do I just put my React files in the Django template folder?我只是将我的 React 文件放在 Django 模板文件夹中吗?

I would use Django REST framework for this.我会为此使用Django REST 框架 It allows you to create JSON (JavaScript Object Notation) APIs using Django.它允许您使用 Django 创建 JSON(JavaScript 对象表示法)API。 This means that your Django/ python models now can be interacted with using javascript.这意味着您的 Django/python 模型现在可以使用 javascript 进行交互。 Now you can use React to do things like query data in your database (through your Django models) and display it on your frontend.现在你可以使用 React 来做一些事情,比如在你的数据库中查询数据(通过你的 Django 模型)并将它显示在你的前端。

It also gives the the added bonus of allowing you to use any frontend framework that reads JSON (most of them do).它还提供额外的好处,允许您使用任何读取 JSON 的前端框架(大多数都这样做)。 For example, you can now build an iPhone app using another frontend framework and use the same Django API that you're using with React as its backend.例如,您现在可以使用另一个前端框架构建 iPhone 应用程序,并使用与 React 作为后端的相同 Django API。 This means you only need to have one backend framework to serve all of your frontend projects.这意味着您只需要一个后端框架就可以为您的所有前端项目提供服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM