How to get Django and ReactJS to work together?

前端 未结 9 1918
无人及你
无人及你 2020-12-04 04:12

New to Django and even newer to ReactJS. I have been looking into AngularJS and ReactJS, but decided on ReactJS. It seemed like it was edging out AngularJS as far as popular

9条回答
  •  醉酒成梦
    2020-12-04 05:02

    Hoping to provide a more nuanced answer than any of the ones here, especially as some things have changed since this was originally asked ~4 years ago, and because many of the top-voted answers claiming that you have to set this up as two separate applications are not accurate.

    You have two primary architecture options:

    1. A completely decoupled client/server approach using something like create-react-app and Django REST Framework
    2. A hybrid architecture where you set up a React build pipeline (likely using webpack) and then include the compiled files as static files in your Django templates.

    These might look something like this:

    Option 1 (Client/Server Architecture):

    Option 2 (Hybrid Architecture):

    The decision between these two will depend on your / your team's experience, as well as the complexity of your UI. The first option is good if you have a lot of JS experience, want to keep your front-end / back-end developers separate, or want to write your entire application as a React single-page-app. The second option is generally better if you are more familiar with Django and want to move quickly while also using React for some parts of your app. I find it's a particularly good fit for full-stack solo-developers.

    There is a lot more information in the series "Modern JavaScript for Django Developers", including choosing your architecture, integrating your JS build into a Django project and building a single-page React app.

    Full disclosure, I'm the author of that series.

提交回复
热议问题