Why React + Django REST Framework Beats Native Django

In the modern web application development process, we can not rely on a single technology or language.

The web has become more interconnected and complex. Each technology, framework, and library have their strengths.

To build a viable web application in this interconnected world, we combine multiple technology stacks.

One of such popular combinations is React and Django.

In this article, I'll explain why I believe a combination of React.js and the Django Rest Framework is superior to Django's own built-in templating engine.

Table of Contents

You can skip to a specific section of this article on React and the Django Rest Framework using the table of contents below:

What is Django Framework?

Django is a high-level Python web framework designed to create web applications faster and easier. This free and open-source framework can be considered a somewhat opinionated framework. Django provides components to handle most web development tasks. However, Django’s decoupled architecture allows developers to select specific components and integrate new ones.

The Django framework is based on the model-template-views architectural pattern. Thus it allows the developers to focus on the program logic and letting the framework handle the low-level implementation details. For Example, Django ORM handlies the database migrations and queries written for Django models reducing the work you have. Also the interoperability of the Django REST framework provides the functionality to easily serialize the database models to RESTful formats.

What is React?

React is an open-source JavaScript library that can be used in front-end developments of both web and mobile applications. React can be used to build both UI components and complete interfaces from combining visual elements, binding data to configuring the logic behind each component.

React has two main variants; ReactDOM for web applications and React Native for mobile application development. Unlike older technologies, modern JavaScript frameworks take over logic processing and document manipulation offering more flexibility and ease of use for developers when creating applications. React distinguishes itself from other JavaScript frameworks because of its flexibility. A developer can simply grab a library and use it in a single webpage or extend the functionality by integrating it with other tools to build complex applications.

Why React + Django REST

Django is a really powerful framework that encourages rapid development and a clean, efficient design. However, it has limits when it comes to creating front end interfaces. Django’s real strength lies in its backend development capabilities. It's major factor is the built-in support for creating the Representational State Transfer(REST) API. The REST API offers a great deal of flexibility. Thus, the Django REST framework is a powerful, scalable, and versatile toolkit for extending the functionality of an application.

When combining React with the Django framework we are offloading most of the frontend development to the React framework. This enables us to leverage the strong suits of each framework; React for frontend development, and Django for backend development.

The Basic principle when combining React with Django is that React will consume the Django REST API to fetch and set data. React with Webpack and JavaScript transpiler Babel will bundle and transpile JavaScript into single or multiple files to be placed on an HTML page. When a page is being loaded, React will communicate with the Django API to fetch the backend data necessary to render the web page.

When connecting React with Django, we rely on Webpack and Babel to ensure seamless functionality.

Babel

Babel is a JavaScript transpiler that essentially makes it possible for a browser to understand JavaScript code like React JSX where it is not supported natively. Babel is based on presets, which are a collection of plugins that allows to transpile specific components. A project can have multiple presets.

Webpack

Webpack simply bundles the codebase with all its dependencies to one or more bundles or files which then can be executed in a browser. The concept of Webpack is that when an entry point, a JavaScript file is defined, it will recursively gather all the dependencies of the entry point file to a single combined file. This eliminates the need to manually add all the dependencies of a project. Additionally, Webpack supports importing of JSON which can be configured to allow imports using loaders for CSS, SASS, etc…

Let us take a look at the features of ReactJS and Django which complement each other when developing a web application.

React Features

Components

The base building block of React is its components. Components can be used for simple tasks like UI functionality (user input), network communications to complex functions like website state management. This modularity offers greater flexibility and ease of use.

Declarativity

React code focuses on what to display rather than the process to display the component. This makes for a compelling developer experience. By focusing on the UI design directly, React framework reduces the frontend development time and provides an easier debugging experience.

Simplicity and Ecosystem

React is a relatively small framework and can be easily configured in a programming environment. And the code-splitting feature helps in reducing the load times by only loading the necessary components.

React framework’s popularity has garnered a huge community actively contributing to the development of the framework. This popularity has created extensive libraries extending the feature set and interoperability of React.

Compatibility

React offers solid backward compatibility with its former versions allowing the developers to use new features while depending on older versions of libraries to maintain the functionality.

Django Features

Versatility

The base Django framework contains almost all the components necessary to build a web application. With seamless integration of all the components, Django provides an up to date and consistent base to build your application. Django ability to work with any client-side framework and deliver content in most formats from basic HTML to JSON and XML allows easy integration with React. Also, Django’s built-in support extends to popular database backends like MySQL and PostgreSQL which further complements the development process.

Security

The Django framework is engineered to be as secure as possible and helps developers avoid common security mistakes. Django, by default, has defenses against common vulnerabilities like SQL injects, clickjacking, Cross-site Scripting (XSS), etc…

One of the key features that help developers to enforce security is Authentication. Django offers user authentication as one of its core features. Django framework supports Session Authentication, Basic Authentication, Token Authentication, Remote User Authentication and can be extended to support other authentication methods. This provides more freedom when creating front end interfaces that can leverage any given authentication to communicate with the backend.

Administration

Django provides a straightforward administration interface right out of the box. That can be consolidated and integrated with Django functions such as Database Administration, User Management, Content Management, etc… This eliminates the need for developers to create an admin interface from the ground up.

Scalability and Portability

Django is based on a component-based architecture where each part of the architecture is independent of others and can be swapped out or modified as needed, increasing application scalability. This clear separation allows scaling to be targeted by increasing hardware resources at specific levels such as caching, database, and applications level.

Django code encourages developers to create maintainable and reusable code. This reduces duplicate code-blocks and reduces the overall code-base. Furthermore, as Django is based on Python the whole framework is platform agnostic. If a need arises, we can simply migrate a backend developed using Django to a completely new platform relatively easily.

Application Architecture when utilizing React + Django

In simplest terms, React will manage the frontend of the application while Django handles all the backend tasks including database interactions.

Django will handle the complex application logic and CURD (Create, Update, Read, and Delete) operations. The Django ORM (Object Relational Mapping) will generate the database objects according to the specified models without having to write SQL queries. Then using the Django REST framework, a developer can provide access to the data via HTTP requests. React will depend on these API endpoints to communicate with the application backend using Token based authentication.

Django can also help with caching by handling dependencies implemented in Manifest files and parse them accordingly. This leads to improved performance of the application.

Meanwhile, React can be responsible for all the frontend components. Essentially providing a beautiful and impressive interface for end-users while reducing some of the server-side rendering requirements like validations which leads to increasing overall performance. The code spitting feature further helps to increase the application performance by loading the application gradually. Downloading only the components and files necessary for the current user interacting while downloading other chucks of the application in the background.

Final Thoughts

React with the Django REST framework can be considered as the best of both worlds. Joining these two powerful frameworks together makes for the most flexible and dependable technology stack for creating web applications. We allow each framework to excel at its core strengths while pairing each framework using the REST API.

Where native Django lacks functionality or requires a complex codebase to achieve the desired result, the React + Diango REST framework comes to the rescue by providing an extensive collection of functionalities and easier implementation. This reduces the overall development time and provides an improved developer experience.

If you enjoyed this article, be sure to join my Developer Monthly newsletter, where I send out the latest news from the world of Python and JavaScript:


Written on January 8th, 2021