Building a backend for a web application is tricky. It involves selecting a language that you are comfortable with and then picking a web framework built on top of that language to avoid writing things from scratch.
There are a ton of web frameworks out there, each designed to address specific needs. The Django web framework addresses all of those needs to a reasonable extent and hence it is preferred by companies like Spotify and Instagram.
## What is Django?
*Fun fact: Django was named after the jazz guitarist* [*Django Reinhardt*](https://en.wikipedia.org/wiki/Django_Reinhardt)*.*
Django is an open source web framework built on top of python. The primary purpose of Django is to enable super fast development of backend applications.
A backend application is nothing but an interface to a database meant for reading the data models and presenting it to a user in a form that they understand. It is also responsible for creation of new entries in the database and updating existing data.
**Why use a web framework?**
If one has to do this from scratch here is what will happen. First you will realise that raw SQL queries would have to be written to manipulate the database and this stops scaling pretty quickly.
You might then resort to writing your own wrapper on top of the raw SQL queries so that you don’t end up repeating a lot of code. Great, but what about security? Anybody could easily run an SQL injection if you let users interface with raw queries.
So now you spend some more developer resources to take care of this problem, but before you can sit back and relax you will realise that you still haven't handled security vulnerabilities like cross site scripting and clickjacking.
Let us assume that you have a very high will power and managed to handle this problem as well. How do you plan to expose your backend to your frontend (web/mobile app)?
You will do so using the HTTP protocol right? Well have you written a library to handle all the nuances involved with accepting a HTTP request in accordance with the protocol, authenticate the origin of the request, and then generate a response after running your business logic?
Of course not! Because it is 2021 and there are so many libraries and frameworks that already do a perfect job of handling all of this.
**Do not reinvent the wheel**
This is where Django or any other framework for that matter comes into the picture. Django is commonly referred to as a “batteries included” solution that takes care of everything I just mentioned and it does it really well.
It’s been written and maintained by very experienced developers who are most definitely smarter than me, so it makes no sense to rewrite everything that they've written. Instead my time is better spent on building on top of Django.
## Why you should use Django
The question is no longer if a web framework should be used or not but whether or not you should choose Django over the other web frameworks.
This is a valid question because Django is not a silver bullet that solves all your problems. Here are few problems that it does solve though.
By virtue of being written in python, the syntax of Django is straightforward and easy to use. It pretty much looks like the english language. Anybody familiar with Python will not have too much trouble picking it up.
Django offers a Object Relational mapping that turns database tables into objects which anybody familiar with OOPS can easily understand.
This also helps abstracting out the database level operations which the developer need not be aware of.
**An admin interface**
Django provides a rich UI out of the box which you can use to perform CRUD operations on your data without writing a single piece of additional code.
You can also add users who will have access to the admin interface and restrict their permissions to define which database models they will have read/write access to.
**Handling security vulnerabilities**
cross site scripting, clickjacking and SQL injections are addressed out of the box.
In addition to this, Django is pretty prompt with introducing new security patches and is quick to notify the community when new vulnerabilities are found.
Writing REST APIs using Django is a breeze. They have excellent libraries to handle HTTP requests and responses.
Django uses a Model-View-Controller architecture, however it prefers to take a different approach to its implementation and is commonly referred to as a Model-Template-View or a MTV architecture.
This is because a Django view is actually a controller that decides how data is accessed and the “view” in MVC actually refers to a Django template which decides how a user sees this data. There is no difference in the “model” part of MVC
This is how MVC works in Django:
Model <--> Model
View <--> Template
Controller <--> View
**An active community**
Django is very well documented and has a huge community that contributes to it on a regular basis.
This reduces chances of getting stuck on a problem that you don't understand. A quick google search is usually enough to remove any blockers.
**Single source of truth**
If your use case demands your frontend and backend code to exist in the same place, Django is a good choice.
There is very less likelihood that further development and support for Django will stop anytime soon, because huge companies have built their products using this framework.
## When not to use Django
Like I said before, Django is not a silver bullet. Here are some reasons why you should not use Django.
**Building a simple app**
If you are building an application that does not require any complicated actions. For instance, you might be building an application that just receives data from a webhook and pushes it to a queue. In such cases I would recommend using [Flask](https://flask.palletsprojects.com/en/1.1.x/) over Django.
**High learning curve**
If your team members are not at all familiar with Python/Django, it’s best to go with a framework they are most comfortable with, otherwise it could delay development to a great extent.
**A strong reason to build everything from scratch**
Sometimes building things from scratch is a better solution than using a framework. But make sure you know what you are doing.
If you go down this path, you have my respect.
**Need for microservices**
You might have a team with a wide range of skills in terms of languages and frameworks. In this case you might have to delegate work by letting each module be built as a separate microservice that will be maintained by a specific person/team.
Django could still be used for this purpose but you wont end up using a lot of features that it offers.
## Closing notes
In my opinion, Django not only helps with rapid development, but it also serves as a great tool to teach people software development. I can say this because it is the first framework I learnt when I started coding.
Django also has a lot of street cred in that many known companies use it. Some of these include
Additionally, [here](https://djangosites.org/) is a list of websites that are powered by Django.
In conclusion, Django covers a wide range of use cases which include high volumes of content and user interactions as well as small scale applications. So no matter what your goal maybe, it is a safe choice to go with.
Originally posted in my [Wholesome Django](https://www.sankalpjonna.com/newsletter) newsletter.
18 thoughts on “Why you should use Django in 2021”
Fully agree! Been learning programming in late 2019 with django.
Syntax is so clean and you its a great framework – among the best.
Because Django is awesome! Next question! 😉
This is an excellent write up, thanks for posting it.
Django is great no doubt, i took course in 2020 but recently I’ve realized that the Web development is dominated by php, laravel, .net etc, infact more than 80% of world’s websites are written in php and it’s framework. As far as Jobs market is concerned Django is not that popular at least in my country, job prospects are relatively lesser.
Great read, I really love Django, out of all the frameworks I know this is my absolute all time favorite.
Regarding small apps, my friend once asked me if he can use Django for creating a simple api that sends a push notification request to onesignal when the api receives a request, I said “you can, but it would be the equivalent of going to a neighborhood fight with a tank” the only time where I don’t even think about using Django is for small apps such as these as it’s so overpowered for them I feel like I’m actually insulting it.
I don’t agree with the assertion that you shouldn’t use Django for a simple app. It’s really not hard to break Django down and just use the parts you need. Read [Lightweight Django](https://www.oreilly.com/library/view/lightweight-django/9781491946275/) – it’s a bit old now but the principles remain the same.
I love this argument in its entirety. To sum it up I would say that if the web app you’re planing on creating uses APIs written in python then you almost definitely ought to use Django. Even if those APIs aren’t written in python they’re likely to use the Http protocol anyway which can be easily parsed as a JSON. I love Django.
Why Django over Laravel?
Lovely right up. I like using Django for building REST API’s.
However for websites and Dashboards, I prefer R Shiny framework. As you don’t need to know HTML and CSS. You can easily scale for Production using Docker and Kubernetes.
Nice article. I love Django it’s a great web framework. Biggest downside I’ve noticed is the coupling of the domain model to the data model. You can’t separate those two without battling the ORM hard and it’s just not worth it. This coupling basically makes it impossible to write unit tests that don’t touch the database without mocking or patching. Bummer, still a great framework though and the ORM is super easy to use and powerful
Why should I use Django over Laravel? The only reason would be the admin interface in every other aspect Laravel is far superior.
The big reason not to use Django: pip; The worst package manager ever created.
These are all good points, and I’m in this sub because I believe Django is pretty much the top app framework out today.
But I’d like to play Devil’s advocate here, to expand the conversation.
> Simple syntax
While true, the same can be said of almost any framework, especially one based on Python.
This has some caveats. First, while Django is technically database agnostic, the volume of small features gained when using Postgres nearly dictates you use Postgres, specifically. This can take some adjustment if you’re coming from SQL or MySQL based products.
Further, NoSQL is becoming more and more popular, and adapting the ORMs is nontrivial, to say the least. In fact, adopting the ORM to NoSQL is such an advanced topic, that you may consider choosing between the two -either Django or NoSQL- rather than trying to shoehorn one into the other.
> An admin interface
I think this is actually one of Django’s weak points; at least out of the box. After working with Wagtail a bit, I’d argue it’s nearly a necessity if you want non-developers logging in to the admin.
And, while Wagtail brings some much needed utility to the admin section and content building parts of Django, it’s also yet another layer with its own quirks and conventions you must get used to. And, to get the maximum benefit, it’s best if Wagtail is part of the project from its initial phases.
> Writing REST APIs using Django is a breeze.
DRF is well thought out, yes. But building out REST in something like APIGateway has less overhead, and can be run serverless (i.e. No server maintenance, instant scalability).
Further, leveraging something like AppSync to write GraphQL offers notable advantages, such as strongly typed schemas, granular data returns, and seamlessly retrieving data from any data source or existing service.
> Handling security vulnerabilities … cross site scripting, clickjacking and SQL injections
Most of that can be handled by a well crafted WAF, and what can’t is usually handled by simple libraries.
To that end, Django arrives with its own security risks. The Django team does a good job of staying on top of these issues, but it requires diligence on the side of the developer, which makes Django a liability.
Removing a request entirely before the server even thinks about processing it -via WAF- can increase performance, and allow more granular control.
All that said, there are also points to consider for the “don’t use Django” arguments.
> Building a simple app
“Simple” is subjective, but in the right hands, Django can be made to be far more lightweight, efficient and scalable than, say, WordPress, which is largely considered the poster child for “simple” websites.
> High learning curve
I disagree here, as long as you’re comparing it to other frameworks. Django’s documentation is among the best, and the architecture is well thought out. To avoid the battles, I won’t name names, but Django is notably more approachable and easy to learn compared to other frameworks, especially in other languages.
> Need for microservices
I’d argue this is one of Django’s strengths, The sites listed which give Django street cred are all run on (hugely complex!) microservice architectures.
In fact, Django’s ability to play nice in SOA is a prime reason to consider it. Few other frameworks can go from monolithic to microservice so gracefully.
The thing I personally dislike about Django is how tightly integrated admin app is to the core of the framework.
Also lack of support for reusable widgets/components seems a little bit odd too. Make me repeat a lot when my interfaces frequently need widgets from other modules.
PSA, I chose Django for my latest project, but for different reasons.
It’s quite interesting to see the framework trends on Stackoverflow today: Ruby on Rail has been losing momentum, and Django is quite the opposite.
I started my first Django project about 10 years ago and today I’m still using it a lot for tons of different projects:
\- Small or big backoffice websites
\- Backend APIs for Vue.js
\- Scrapers/crawlers (even if, for projects requiring strong parallelism, I’m now using Go)
\- I even used it with HTMX (new version of Intercooler.js) in my latest [nlpcloud.io](https://nlpcloud.io) project
It always had my back!
Hi, anyone recommend me an angular and Django video tutorials on youtube, I did not found any full tutorial on youtube(in english).
Unless you actually want to get a job….Then choose something else. That list of websites powered by Django is mostly garbage-looking sites. Looking like most using the same terrible bootstrap.
I just saw this post and I am wondering how does Django relate to Laravel ? I have experience and knowledge of Laravel and want to know if I can easily transition it into Django as I would love to start using Python again.