Development

Django’s Place in a Web Development World Ruled by React

5 Apr 2021 7:46am, by

Web development in 2021 is so dominated by JavaScript and its many frontend frameworks, it’s sometimes easy to forget that the traditional server-side web application architecture is not only alive and well — but thriving. 55.3% of Django developers who took the annual StackOverflow survey said they will continue to use it; and 49.3% said the same of Ruby on Rails. Django and Ruby on Rails — two of the leading backend web development frameworks and both open source — have been on the scene since the mid-2000s. They’re no longer the trendy young bucks in web development, but they are still the seventh and 11th most-loved web frameworks respectively.

In a column earlier this year, I discussed how Basecamp — the company that created Ruby on Rails — is adapting to frontend trends with a newly released framework called Hotwire. One of the goals of Hotwire is to reduce the amount of JavaScript code being used, by “sending HTML instead of JSON over the wire.” It’s both a challenge to the JavaScript reliance of leading frontend frameworks like React and Next.js, but also a way for Ruby on Rails to stay relevant.

To find out the Django perspective on our current JavaScript-heavy web landscape, I reached out to one of its co-creators — Simon Willison. Nowadays Willison is focused on his latest project, Datasette, an open source tool he created for exploring and publishing data. But he’s best known for co-creating Django in 2003, as a Python framework to help developers build complex, database-driven websites. At the time Willison and his co-creator Adrian Holovaty were working for a small US newspaper, so the idea was to help the paper’s writers and editors easily put content into a database that would then get published to the website.

Richard MacManus
Richard is senior editor at The New Stack and writes a weekly column about web and application development trends. Previously he founded ReadWriteWeb in 2003 and built it into one of the world’s most influential technology news and analysis sites.

Over the past fifteen or so years, Django has become an integral part of a number of massive-scale internet products, such as Instagram, Pinterest and Spotify. As an Instagram engineer noted in September 2019, “Instagram Server is a Python monolith with several million lines of code and a few thousand Django endpoints.”

What’s less known is that Django is sometimes used alongside the likes of React and Vue, two of the leading frontend frameworks of today. This shouldn’t be a surprise — Instagram clearly uses both Django and React, since the latter is a project run by its parent company Facebook. But judging by a recent Twitter comment by Willison, it’s a common misconception that Django can’t work with JavaScript frontend frameworks.

“The technically correct answer,” Willison told me when I asked him about this, “is that a backend framework like Django works with any frontend framework, because of separation of concerns: if a web framework can respond to HTTP requests with JSON and HTML, it can be used with anything.”

However, he noted there are aspects of the Django/JS framework relationship that confuse people, “especially newer developers.”

“Things like JWT [JSON Web Token] for authentication are a hot topic — core members of the Django project think JWT has a flawed design and have argued against shipping support for it in Django core, leaving it to third-party packages. But lots of developers want to use JWT and have a hard time figuring out how to do it.”

The default solution, he said, is to use the Django REST framework, a toolkit for building Web APIs. This toolkit includes support for JWT. An alternative, which he himself often uses, is to write standard Django views that return JSON.

The case for pairing the two frameworks would be to use React to handle the user interface, fetching data from the Django backend as necessary. For example, this tutorial shows how to build a to-do application, with React making HTTP requests to the Django REST API in order to fetch and set data.

The Problem with Single-Page Applications (SPAs)

Although Django and React can play well together, there’s been a lot of debate in recent years about how far to push the React model of application development. Some think that React has introduced too much complexity into web development and is now overused.

In an influential blog post last May, software engineer Tom MacWright wrote that the “emerging norm for web development is to build a React single-page application, with server rendering.” Server-side rendering (SSR) means to run JavaScript frontend code on the backend, in order to pre-populate an HTML page (which is then sent to the client, where typically further JavaScript code runs to make the page interactive). MacWright says this approach is unnecessary for many use cases, noting that it can produce “messy optimizations” and issues with data fetching.

According to MacWright, data fetching is “really important and really bizarre in React land.” Although he likes GraphQL as a way to fetch data, he says that “for a React component that loads data with fetch from an API, the solutions have only gotten weirder.”

Willison agrees with MacWright about the overuse of SPA.

“In the past 5-10 years,” Willison said, “it seems that new developers have grown up in a world where SPA is the default approach to building pretty much anything. I think it’s a terrible default for many projects.”

As well as being of dubious technical benefit, Willison thinks some SPA projects can drag on for longer than necessary.

“I’ve seen SPA projects take months longer than I would expect the same project to take if it was built with boring old JavaScript-free HTML forms.”

Modern Web Development Shouldn’t Mean Ignoring the Classics

Getting back to Tom MacWright’s article, he (rather unfairly, I think) labeled Django “uncool” and suggested it was “outside the norms of modern web development.” But if you accept his argument that the React single-page application has many problems, then surely a boring old Django web page or app would suffice in many use cases — at least for cases that don’t require a complicated user interface.

But equally, in Django and React we have two frameworks that should be complementary to each other — one is a backend framework, the other is for the frontend. As Simon Willison pointed out, there are relatively simple ways to have a React-built user interface that gets data from a Django backend as needed.

As in many areas of twenty-first century life, sometimes we just over-complicate things in web development.

Feature image via Pixabay.

A newsletter digest of the week’s most important stories & analyses.