How to get Django and ReactJS to work together?

前端 未结 9 1839
无人及你
无人及你 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.

    0 讨论(0)
  • 2020-12-04 05:07

    I feel your pain as I, too, am starting out to get Django and React.js working together. Did a couple of Django projects, and I think, React.js is a great match for Django. However, it can be intimidating to get started. We are standing on the shoulders of giants here ;)

    Here's how I think, it all works together (big picture, please someone correct me if I'm wrong).

    • Django and its database (I prefer Postgres) on one side (backend)
    • Django Rest-framework providing the interface to the outside world (i.e. Mobile Apps and React and such)
    • Reactjs, Nodejs, Webpack, Redux (or maybe MobX?) on the other side (frontend)

    Communication between Django and 'the frontend' is done via the Rest framework. Make sure you get your authorization and permissions for the Rest framework in place.

    I found a good boiler template for exactly this scenario and it works out of the box. Just follow the readme https://github.com/scottwoodall/django-react-template and once you are done, you have a pretty nice Django Reactjs project running. By no means this is meant for production, but rather as a way for you to dig in and see how things are connected and working!

    One tiny change I'd like to suggest is this: Follow the setup instructions BUT before you get to the 2nd step to setup the backend (Django here https://github.com/scottwoodall/django-react-template/blob/master/backend/README.md), change the requirements file for the setup.

    You'll find the file in your project at /backend/requirements/common.pip Replace its content with this

    appdirs==1.4.0
    Django==1.10.5
    django-autofixture==0.12.0
    django-extensions==1.6.1
    django-filter==1.0.1
    djangorestframework==3.5.3
    psycopg2==2.6.1
    

    this gets you the latest stable version for Django and its Rest framework.

    I hope that helps.

    0 讨论(0)
  • 2020-12-04 05:14

    The accepted answer lead me to believe that decoupling Django backend and React Frontend is the right way to go no matter what. In fact there are approaches in which React and Django are coupled, which may be better suited in particular situations.

    This tutorial well explains this. In particular:

    I see the following patterns (which are common to almost every web framework):

    -React in its own “frontend” Django app: load a single HTML template and let React manage the frontend (difficulty: medium)

    -Django REST as a standalone API + React as a standalone SPA (difficulty: hard, it involves JWT for authentication)

    -Mix and match: mini React apps inside Django templates (difficulty: simple)

    0 讨论(0)
提交回复
热议问题