10 reasons to use Django in 2020

by Mauro Bianchi - 09/01/2020
Python
Django

When we develop the server side of our web applications at INMAGIK, the python based Django web framework is our preferred choice. Django has become a strong technological foundation for us, with lots of success stories in our last three years of web development.

Personally, I've been following and using Django from my very first steps in web development, around 2007, and it helped me a lot, not only as a tool to build websites, but also as a huge resource for learning about web development and python programming.

In this short, hopefully not too technical post, I'll try to give you ten reasons to consider this framework. Of course, like every other tool and library, there might better solutions for each use case, but if you're developing a website and accessing mostly SQL databases with any other language or framework, you should give Django a chance.

Moreover, this is a great time for the django framework, as it has reached stability and provides a great set of features, still being in a very active phase of development with great plans for the near future.

1. It's Python and it's pythonic

Python remains, in my opinion, one of the most readable programming languages. It's available for many platforms and operating systems and has a very powerful standard library. Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles for Python's design:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

(if you have python installed you can start an interpreter and type import this see the Zen of Python in your python console).

One of reasons I like Django so much is that it is really "pythonic" and makes great use of the language features, such as metaclasses used for implementing the django ORM, the database access layer.

In general, the Django APIs, are well structured, elegant and allow a very clean and concise programming style, stressing out very much the DRY principle (Don't Repeat Yourself).

2. License, release process and long term support

Django is an open source framework coming with a very permissive license in terms of commercial usage. With its first release in 2005, it's a very mature project and it's maintained by an awesome group of volunteers, the Django software foundation.

The project provides a well documented release process, with long term support releases (LTS) that will get security and data loss fixes applied for a guaranteed period of time, typically three years, a reasonable amount of guaranteed lifetime for a modern web application.

As a long term django user I can say that the majority of the API is probabily stable since 1.0, with really smooth changes between, well anticipated by a clear deprecation policy.

3. The docs

Since the very beginning of the project, django documentation has always been complete and very user friendly, also for newcomers in web development, thanks to its many examples and tutorials.

Compared to other open source frameworks, we had the need to look into the source code of django just a few times in our everyday use, as the documentation covers all the django APIs is a very detailed way, from the very basic use case to the most advanced ones. As explained in the docs, django documentation offers different levels of reading:

  • Tutorials take you by the hand through a series of steps to create a Web application.
  • Topic guides discuss key topics and concepts at a fairly high level and provide useful background information and explanation.
  • Reference guides contain technical reference for APIs and other aspects of Django’s machinery. They describe how it works and how to use it but assume that you have a basic understanding of key concepts.
  • How-to guides are recipes. They guide you through the steps involved in addressing key problems and use-cases. They are more advanced than tutorials and assume some knowledge of how Django works.

4. Modular and extensible

The Django framework contains different python packages, being effectively many different frameworks for addressing the various tasks of web development in a modular way.

Between many others, we have for example:

  • the ORM for database access and abstraction
  • the caching framework
  • the template engine
  • the sessions and messages frameworks
  • the forms framework
  • authentication backends
  • class based views
  • storage backends
  • the gis framework
  • ...and more

Such modularity helps developer building different kind of web applications, with rich APIs for dealing with all the common use cases. These sub-frameworks and packages are sometimes referred as "backends", as they abstract some groups of functionalities, that can be completely turned off or swapped in favour of other implementations.

Moreover, all these packages and "backends" provide great APIs allowing easy composition of features, extension and custom implementations for almost every part of the Django framework as a whole.

One notable example of these frameworks is the GIS framework django.contrib.gis, a very powerful set of tools to handle geographic information, that comes with the standard django installation.

5. Ecosystem and reusable apps

The python standard library is one of the most complete I know of, but the success of Python, in particular in the field of data science, is highly related to the availability of third party packages (for example numpy for advanced math and pandas for data analysis). Here you can find an exhaustive list of interesting python packages.

In a django project, the separation of concerns is encouraged by letting you split the various features of your web applications into "reusable apps", that are just python packages. This helps a lot with code organization and to allow reuse of code. These apps can live just in your project code, or be distributed like any other python package and installed in the project as external dependencies.

This approach has led to the proliferation of many specialized reusable apps, from very low level features to ready-to-use intefaces.

These some of the notable third parties app we often use at INMAGIK:

  • Django rest framework is one of the most used libraries to build REST APIs with django. It's the base of our API development (REST-like and not) and it's another great example of great API design. We learned a lot about web services implementation from this library, and using the rest framework might be a good reason to use django.
  • Django channels: "channels augments Django to bring WebSocket, long-poll HTTP, task offloading and other async support to your code, using familiar Django design patterns and a flexible underlying framework that lets you not only customize behaviours but also write support for your own protocols and needs."
  • Django reversion: an extension to the Django web framework that provides version control for model instances.
  • Django crispy forms: DRY Django forms
  • Django storages: A single library to support multiple custom storage backends for Django
  • Django rq: Integration for Redis Queue
  • Django imagekit: automated image processing for Django (for automatic thumbnails and image processing)
  • Django autocomplete light: a fresh approach to autocomplete implementations

The django packages is a complete directory of django-related packages and you can find more inspiration in the awesome django repo on github.

6. The ORM

Django is a very general framework for building web applications, but it really shines when dealing with SQL databases.

The ORM (Object Relational Mapper) is one of the most powerful parts of Django, and can be described as a layer between the database(s) and your application, with an elegant and powerful API for defining, accessing and maintaining your data from in python code.

These are some of the key features of the ORM:

  • it's backend agnostic: with core support for sqlite, postgresql, mysql, mariadb and oracle (plus well known 3rd party packages to support IBM DB2, Microsoft SQL Server, Firebird and ODBC). As long as not you're not writing directly specialized SQL, your code should work with any of the supported databases.
  • it provides a declarative data model: the schema for data models is defined in python classes rather than by CREATE TABLE... SQL statements. As it's python code, this model is also available for introspection in your application (for example, you can cycle all the fields of a data model or know the data type of a certain field)
  • you will normally write no SQL to access and modify your data, with the ability to perform complex queries, data aggregation and full transaction control (you still can write SQL, if you really need).

At INMAGIK we normally use PostgreSQL as database backend, for its reliability, extensions, great support and availability of commercial support and providers. Django has an extended support for the PostgresSQL backend, with its django.contrib.postgres package, that contains model fields and form fields for a number of PostgreSQL specific data type.

During our project development at INMAGIK, we met two great usecases for django + postgres:

  • geographical information systems based on django.contrib.gis and the django.contrib.gis.db.backends.postgis backend, that will give you access postgis, a powerful postgres extension for dealing with geographic data, from the django ORM.
  • data models with JSON fields: postgres, from versions 9.2 and 9.4 comes with a specific "JSON" field that allows storage of json data structures in a table column, with the ability to search and index specific json paths. This can be very useful if you have to deal with partially structured data. This approach can be used in many cases where a natural solution would be using a NoSQL backend.

7. Migrations

Migrations are still part of the Django ORM, but this feature deserves a point on its own.

The SQL (relational) data model has many advantages when writing applications, as it provides a very structured way to access data, allowing us to define constraints about the shape and relations of records. Once a record has been "accepted" by a well structured SQL database, we can safely rely on our constraints to build complex logic that manipulates data.

This comes at the cost of using a dedicate set of SQL instructions for data definition (CREATE/ALTER TABLE, CREATE/ALTER INDEX, CREATE/ALTER CONSTRAINTS, ...) and imperatively maintaining our tables as the application evolves.

Django features the powerful migrations framework, born as a third party app called South and merged into the django core by Andrew Godwin, the principal maintainer of South and django core team member. This framework helps you by detecting changes in your data models declarations and generates automatically SQL statements to adapt the database schema. It also provides the ability to create "data migrations" to apply transformations to your existing data in a predictable way.

This is a real killer feature of the django framework, and I never found something comparable in other web frameworks. The SQL data model is something that will evolve with your application (very fast in the development stage, but also in production when you will add new features), so be prepared to manage these changes.

Take a look at the topic guide on migrations from the django docs for all the details.

8. The admin site

Django comes with the django.contrib.admin package, a reusable app that enables a web based admininistration interface for your data models with just a few lines of code. The django admin is very interesting for many reasons:

  • you can try it a zero cost: its enabled by default in django projects and you need 4 lines of code to get a full CRUD interface for a data model.
  • it leverages and shows all the power of the django ORM
  • it's a beautiful example of a configurable web application, with simple API and a clean user interface.
  • it's an interesting administration tool, in particular if in your web application you only use the ORM to access your models, as all the resources are available with the same level of abstraction.

The admin was not design to be used by final users, but in our experience it may be a good option, if you need a simple administation interface for some advanced users and you don't have a very complex data model. During development the admin interface may also be used for debugging and to put some data in your database.

The django admin site API provides lots of options to customize your administration site, and there are many third party packages to enhance the admin interface and user experience.

9. Async is coming

Django was initially designed to handle one-way, syncronous, client-server "request-response" interactions based on the HTTP protocol. Django's primary deployment platform has always been WSGI, the Python standard for web servers and applications: the Web Server Gateway Interface is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language.

This kind of design makes difficult to implement non-HTTP protocols such as websockets, common in modern web apps. To overcome such limitation, the Channels project was born. This project, which we are using in some of our projects for websocket support, "takes Django and extends its abilities beyond HTTP - to handle WebSockets,chat protocols, IoT protocols, and more. It’s built on a Python specification called ASGI."

Beyond websockets and other async protocols, async programming gives us many opportunities in terms of performance optimization and flexibility, and is one of the reasons of success of nodejs, the popular javascript runtime, which has an event-driven architecture capable of asynchronous I/O.

Back to the django world, the DEP 0009: Async-capable Django by Andrew Godwin, proposes to add "support for asynchronous Python into Django, while retaining synchronous Python support as well in a backwards-compatible fashion ..." and that "..in the near future Django will add on support for asynchronous views, middleware, ORM, and other key pieces."

This process has started with the 3.0 relase of django, with ASGI support: as the docs say "Django 3.0 begins our journey to making Django fully async-capable by providing support for running as an ASGI application".

This is great news for all django users, as it opens new use cases and applications for the framework in the near future.

10. Success stories

Django is part of the stack of many successful websites and applications used by millions of users, such as:

  • EventBrite
  • Bitbucket
  • Pinterest
  • Instagram
  • The mozilla support site
  • Disqus
  • Prezi
  • The Washington Post

Wrap-up

Django is an awesome python based web framework, mature, stable and packed with features allowing to build a wide range of web applications, from REST APIs and web services to server-side rendered websites.

At INMAGIK, in 2020 we'll continue to invest on django for and use in our stack, as it helps us building maintenable and extensible web apps and services for our clients.

Let us know what you think by tweeting at @inmagiklabs.