How to get Django and ReactJS to work together?

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

提交回复
热议问题