Sign in
Log inSign up
React + Django REST framework

React + Django REST framework

Valentino Gagliardi's photo
Valentino Gagliardi
·Feb 19, 2018

I know, it's been a long time since you heard from me.

I'm a bit busy working with React and Django. Yes, Django.

"Valentino, you were supposed to talk about Node.js all the way down!"

You're right. I should talk about Node.js.

But when the client wants to see a prototype straightway it's better to use a web framework that gets things done.

And trust me, Django is great when it comes to development speed.

But how do you structure a Django project with React?

Should React mount a component for each Django template? Should React take over the routing?

Each approach has its own quirks. Here's my catch:

Go full Django templates if the application does not require Javascript and there is no value in adding React.

If you're building a SPA instead, let React manage the frontend whenever it's possible.

My sweet spot is Django REST framework for providing API endpoints. With React in its own app called "frontend".

You can find an example in my Github repo: Django REST framework + React (blog post coming soon).

How about authentication? If you want to sleep at night forget about token authentication and use the Django one instead.

You can protect the API endpoint with rest_framework permissions. From then use the Django built-in authentication to handle user registrations and logins. Once a user is authenticated show him the React frontend. That's all.

I know it works because I'm using it in production.

I'm curious. Are you already using Django and React?

Cheers!