Django和Reactjs [英] Django and Reactjs

查看:61
本文介绍了Django和Reactjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是React JS的新手,我已经看到很多关于使用React和Django使用 django-webpack-loader 和webpack-bundle-tracker的文章,我必须说我一直关注他们,但没有成功.首先,我很困惑.我要运行哪个服务器?

I'm new to react js and I've seen a lot of write-ups on using react and Django using django-webpack-loader and webpack-bundle-tracker and I must say that I've followed them but no success. First of all, I'm confused. Which server do I run?

py manage.py runserver

webpack --config webpack-config.js -watch

django-webpack-loader是做什么的?

What does django-webpack-loader do?

我什至不知道我是否已经问过所有我应该问的问题.我所知道的是,我没有成功地将它们链接起来

I don't even know if I've asked all the questions I should ask. All I know is that I've not been successful in linking them

推荐答案

几个月前,我处于同样的情况,从您的问题中得出的结论很多,我想您正在努力用React设置Django.我想分享一下我的实现(这是每个人都建议使用的一种标准.)

I was in the same situation months back, As much I concluded from Your problem, I guess you are struggling to setup Django with React. I would like to share my implementation(it is a standard one everyone recommends to use it.)

相应地遵循以下步骤:-

Follow These Steps accordingly:-

  1. 将您的前端与后端分开,这意味着您需要准备REST API作为后端不要使用Django模板,只需创建一个Restful API,该API会在views.py的每种方法中返回一些JSON数据.您可以使用 DJANGO REST FRAMEWORK 制作rest-API,它提供许多功能,还需要通过 pip install django安装 CORS-HEADERS -cors-headers 并通过以下方式在 settings.py 中更新您的INSTALLED_APPS:

  1. Separate your Frontend with your Backend, What this means is You need to prepare the REST API as your backend Do not use Django templates just make a Restful API which returns some JSON data in every method of your views.py.You can Use DJANGO REST FRAMEWORK for making rest-API,it provides lots of functionalities,You will also need to install CORS-HEADERS by doing pip install django-cors-headers and update your INSTALLED_APPS in settings.py by:-

INSTALLED_APPS = [
.......,
.......
'corsheaders', 
]

cors标头的整个目的是允许发出不在同一原始位置上运行的请求.由于您的前端与后端位于不同的端口上.

The whole purpose of cors headers is to allow to make a request which is not running on the same origin. Since your frontend is on a different port from your backend.

使用完API后,立即使用React使前端可以在需要时调用该API.

As soon you finish with your API, Now make your frontend with React to call the API whenever you need.

api服务器将在类似 http://localhost:8000 ,前端服务器将运行 http://localhost:8080 .

The api server will be running on something like http://localhost:8000 and frontend server will run http://localhost:8080.

因此,您必须访问端口号. 8080 来查看应用程序,并查看API的端点,您需要访问端口号. 8000

So You have to visit the port no. 8080 to view the app.and to see the endpoints of the API you need to visit port no. 8000

您可以看到我的项目 Django React博客保持与Django的反应,这也可以为您提供帮助.

You can see one of my project Django React Blog that how to maintain react with Django, that can also help you.

请提出您的疑问,尽管我建议您采用这种架构,因为无论您切换后端框架还是前端框架,都将保持不变,请让我知道其中是否有问题

Please Raise your doubts if any, though I will suggest to follow this architecture because this will remains the same whether you switch your backend framework or frontend framework, Do let me know if something is wrong in it

这篇关于Django和Reactjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆