How to connect Django with React? - a1k89/Blog GitHub Wiki

Hi, everyone.

In one of the last project in our company we made great todo application with boards, chat, tasks, attachments, notifications and many other features.

On client side I choose React.

And one of main truble for me - how to connect React + Django? What's the best way to do it?

Prepare

  • Django project with ready to start
  • React application with ready to coding

Solutions

  1. Only SPA
  • You create 2 separated parts React and Django
  • Django make Rest API
  • React loaded. Then get data through Rest API
  1. SPA as part from Django
  • You create Django project
  • You prepare all (or initial data) on Django
  • You render props
  • React loaded. Then get data from props and then works via RestAPI (create/update/change)
class HelloWorld(DetailView):
    template_name = '/path/to/your/template.html'

    def get_context_data(self, **kwargs):
        props = {
            // your props/data
        }

        context = {
                'component': 'overview.js',
                'title': 'Hello World',
                'props': json.dumps(props)
            }

        return context