Django’s Place in a Web Development World Ruled by React
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.”
“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.
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.
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.